ska*_*v85 5 header-files visual-c++
我的简单类不会在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_
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
Cha*_*had 14
将它放在头文件中,并使用命名空间作为字符串用法的前缀std.
标题:
#include <string>
class engineer
{
std::string company;
};
Run Code Online (Sandbox Code Playgroud)
在实现文件(.cpp)中,您可以为名称添加前缀或具有using指令.
执行:
using namespace std; // using directive, no longer need to use std::
Run Code Online (Sandbox Code Playgroud)
避免将using指令放在头文件中,因为这会污染全局命名空间,并且可能导致在您可能希望使用的其他库中命名冲突时出现问题.
| 归档时间: |
|
| 查看次数: |
17485 次 |
| 最近记录: |