visual-c – 我在哪里将字符串库包含在我的C头文件中
发布时间:2020-12-16 10:30:04 所属栏目:百科 来源:网络整理
导读:我的简单类不会在Visual Studio中编译.在我将字符串公司成员和getter方法getCo()添加到它之前,它工作正常.我想我需要将#include字符串标准库放在某个地方,但我不知道在哪里.知道在哪里?在我的头文件中,我有: #pragma once#ifndef ENGINEER_H_#define ENGIN
我的简单类不会在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指令放在头文件中,因为这会污染全局命名空间,并且可能导致在您可能希望使用的其他库中命名冲突时出现问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |