C++;接口(标题)和实现文件赢得';行不通 我正在使用C++类,并且由于某种原因,我无法得到我的头文件中的类来处理我的一个程序。我使用的是Visual Studio 2017,我使用Visual Studio中的解决方案资源管理器将头文件和实现文件与测试文件一起添加

C++;接口(标题)和实现文件赢得';行不通 我正在使用C++类,并且由于某种原因,我无法得到我的头文件中的类来处理我的一个程序。我使用的是Visual Studio 2017,我使用Visual Studio中的解决方案资源管理器将头文件和实现文件与测试文件一起添加,c++,class,header-files,C++,Class,Header Files,我的程序中有两个构造函数,一个是默认的,另一个不是。我试着从每个文件中删除第二个文件,程序运行了,但我无法让它正常工作 头文件: #pragma once class BuckysClass { public: BuckysClass(); BuckysClass(string); void coolSaying(); }; #include "stdafx.h" #include <iostream> #include "BuckysClass.h" #include <s

我的程序中有两个构造函数,一个是默认的,另一个不是。我试着从每个文件中删除第二个文件,程序运行了,但我无法让它正常工作

头文件:

#pragma once
class BuckysClass {
public:
BuckysClass();
BuckysClass(string);
void coolSaying();
};
#include "stdafx.h"
#include <iostream>
#include "BuckysClass.h"
#include <string>
using namespace std;

BuckysClass::BuckysClass() {
   cout << "Bucky is ";
}
BuckysClass::BuckysClass(string x) {
   cout << x;
}
void BuckysClass::coolSaying() {
   cout << "preachin to the choir" << endl;
}
#include "stdafx.h"
#include <iostream>
#include <string>
#include "BuckysClass.h"
using namespace std;

int main() {

BuckysClass buckysObject;
buckysObject.coolSaying();

BuckysClass buckysObject2("Bucky is not ");
buckysObject2.coolSaying();
system("Pause");
return 0;
}
实施文件:

#pragma once
class BuckysClass {
public:
BuckysClass();
BuckysClass(string);
void coolSaying();
};
#include "stdafx.h"
#include <iostream>
#include "BuckysClass.h"
#include <string>
using namespace std;

BuckysClass::BuckysClass() {
   cout << "Bucky is ";
}
BuckysClass::BuckysClass(string x) {
   cout << x;
}
void BuckysClass::coolSaying() {
   cout << "preachin to the choir" << endl;
}
#include "stdafx.h"
#include <iostream>
#include <string>
#include "BuckysClass.h"
using namespace std;

int main() {

BuckysClass buckysObject;
buckysObject.coolSaying();

BuckysClass buckysObject2("Bucky is not ");
buckysObject2.coolSaying();
system("Pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括“BuckysClass.h”
#包括
使用名称空间std;
BuckysClass::BuckysClass(){

cout这里是测试文件的更多图片和我得到的错误消息,我无法在上面发布


您的头文件BuckysClass.h没有包含
头文件

加:

#包括
使用名称空间std;

您应该从相应的.cpp文件中删除这些include,并将BuckysClass.h保留在cpp文件中。

语法错误:标识符“string”

#include <string>//include the string header
class BuckysClass {
public:
  BuckysClass();
  BuckysClass(std::string);//add the namespace identifier
  void coolSaying();
};
#include//include字符串头
班克斯班{
公众:
BuckysClass();
BuckysClass(std::string);//添加名称空间标识符
虚无的冷语();
};

你能包含实际的代码而不链接到图片吗?是的,等等。你展示了该类本身的代码,但没有展示试图使用该类的代码。你的实际问题是什么?你说它不起作用,但它如何不起作用?是的,很抱歉,我还没有10个声誉点,因为我刚刚完成了我的测试我只添加了2张图片。我添加了测试文件和下面的错误消息,我自己回答了XD。抱歉,麻烦。第28行调用带有字符串参数的构造函数。没有这个,C++试图将它与默认复制构造函数匹配。好吧,这有道理。你认为我应该做什么???你是亲。grammer.告诉我你想做什么!Harrison,关于堆栈溢出的重要说明。这是一个问答网站,不是论坛。有一个问题。与问题相关的所有内容都进入问题。其余是评论和答案。评论应该用来要求澄清或提出与问题无关的观点,但他可能会有所帮助。aNSWER只能用来回答问题。这并不能回答问题。Pfft我现在觉得有点笨:)。谢谢Shashank!我们能不能建议初学者在标题中使用命名空间std写