Re: error C2678, string "+" by James
James
Wed May 05 14:30:18 CDT 2004
To explain what happened there, a "parts" of the full std::string is
needed for some other areas of the C++ standard, so if you use one of those
other classes (iostreams, for example, or exception), they include a subset
of std::string in their headers.
So if you'd just written:
#include <iostream>
int _tmain(int argc, _TCHAR* argv[]) // this comes automatically when I
created the project
{
std::cout << "Hello, World!" << std::endl;
std::string s1="abc";
std::string s2="def";
std::string s3;
s3 = s1 + s2;
}
you would have gotten the exact same error (because iostream defines
std::string, but <string> is needed for the operator+.)
Now, when you include the <stdafx.h>, everything before it is forgotten, and
replaced by the contents of the precompiled header.
My bet is that you have <iostream> in your <stdafx.h>.
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Tony" <anonymous@discussions.microsoft.com> wrote in message
news:A8F8B7A8-091C-4BFF-AB30-EB0A19933CD9@microsoft.com...
> Hello,
>
> I am learning Object Oriented Programming with C++.
> I am using Microsoft Visual C++ 2003.
>
> I got the following error message when I tried to complie the following
piece of codes:
> Error Message: error C2678: binary '+': no operator found which takes a
lefthand operand of type 'std::string'
>
> Sample Codes:
>
> #include <iostream>
> #include <string>
>
> #include "stdafx.h" // this comes automatically when I created the
project
> int _tmain(int argc, _TCHAR* argv[]) // this comes automatically when I
created the project
> {
> std::cout << "Hello, World!" << std::endl;
>
> std::string s1="abc";
> std::string s2="def";
> std::string s3;
> s3 = s1 + s2;
> }
>
>
> Can someone point me to the right direction please?
> Should I be using a different option when I created a new project? (I am
using Win32 console application I think)
>
> Thanks.
> Tony