我的简单类无法在 Visual Studio 中编译。在我向它添加字符串公司成员和 getter 方法 getCo() 之前,它起作用了。我想我需要将#include 字符串标准库放在某处,但我不确定在哪里。知道在哪里吗?在我的头文件中,我有:
#pragma once
#ifndef ENGINEER_H_
#define ENGINEER_H_
class engineer {
int years;
string company;
public:
engineer(int years);
~engineer(void);
int getYears();
string getCo();
};
#endif ENGINEER_H_
在我定义类的 CPP 文件中,我有:
#include "StdAfx.h"
#include "engineer.h"
engineer::engineer(int y, string c){
years = y;
company = c;
}
engineer::~engineer(void) {
}
int engineer::getYears() {
return years;
}
string engineer::getCo() {
return company;
}
最佳答案
把它放在头文件中,并用命名空间 std
作为字符串使用的前缀。 .
标题:
#include <string>
class engineer
{
std::string company;
};
在实现文件(
.cpp
)中,您可以为名称添加前缀或添加 using
指示。执行:
using namespace std; // using directive, no longer need to use std::
避免放置
using
头文件中的指令,因为这会污染全局命名空间并可能导致您可能希望使用的其他库中的命名冲突问题。
关于visual-c++ - 我在哪里将字符串库包含在我的 C++ 头文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7449817/