售前咨询
技术支持
渠道合作

C++的errorC2039和C2679的解决

菜鸟最近接触C++,遇到了两个问题,记下来解决办法。

这里贴出练习的代码

#include “stdafx.h”
//#include <iostream>
//#include <string>
#include “windows.h”

class Hello{
public:
void sayHello(){
printf(“Hello jikexueyuan\n”);
}

void sayHello(std::string name){
std::string str = “Hello “;
str += name;
std::cout << str << “\n”;
}
};

运行之后提示错误:
错误 1 error C2039: “string”: 不是“std”的成员
于是我把#include <iostream>加到了全部代码的前面,但是发现依然有错误C2039,但是在错误C2039之前有一个warning
警告 1 warning C4627: “#include <iostream>”: 在查找预编译头使用时跳过
后来发现,#include <iostream>和#include “stdafx.h”是有先后顺序的,而且#include “stdafx.h”需要放到全部代码的前面。
调整了顺序之后再次运行,这次之前的错误没有了,但是有新的错误
错误 1 error C2679: 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
百度之后在前面加上#include <string>解决了这个问题。

上一篇:

下一篇:

相关文章