请高手找下错,真不知道那错了?解决方法
请高手找下错,真不知道那错了?
#include <iostream>
using namespace std;
#include <cstring>
class Person{
private:
string const name;
int age;
public:
Person():name("noname"),age(0)
{
cout << "noname,age=0" << endl;
}
Person(string const& name,int age):name(name),age(age)
{
cout << "name=" << name << ". age=" << age << endl;
}
void show()
{
cout << "My name is" << name << ". " << age << endl;
}
};
int main()
{
Person a;
Person b("wangdefen",49);
a.show();
b.show();
return 0;
}
--------------------Configuration: constructor - Win32 Debug--------------------
Compiling...
constructor.cpp
D:\Learn C\constructor.cpp(16) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
D:\Learn C\constructor.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
Error executing cl.exe.
constructor.obj - 2 error(s), 0 warning(s)
------解决方案--------------------
#include <iostream>
using namespace std;
#include <cstring>
class Person{
private:
string const name;
int age;
public:
Person():name("noname"),age(0)
{
cout << "noname,age=0" << endl;
}
Person(string const& name,int age):name(name),age(age)
{
cout << "name=" << name << ". age=" << age << endl;
}
void show()
{
cout << "My name is" << name << ". " << age << endl;
}
};
int main()
{
Person a;
Person b("wangdefen",49);
a.show();
b.show();
return 0;
}
--------------------Configuration: constructor - Win32 Debug--------------------
Compiling...
constructor.cpp
D:\Learn C\constructor.cpp(16) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
D:\Learn C\constructor.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
Error executing cl.exe.
constructor.obj - 2 error(s), 0 warning(s)
------解决方案--------------------
- C/C++ code
#include <iostream> #include <string> //头文件写错,不用cstring using namespace std;
------解决方案--------------------
#include <cstring> 改为 #include <string>