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>解决了这个问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值