C++ 接口(头文件)和实现文件无法工作

C++ Interface (Header) and Implementation File Won't Work

我正在学习 C++ class,但出于某种原因,我无法让我的头文件中的 classes 在我的程序中运行。我使用的是 Visual Studio 2017,我在 Visual Studio.

中使用解决方案资源管理器添加了我的头文件和我的实现文件以及我的测试文件

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

头文件:

#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;
}

下面是测试文件的更多图片和我得到的错误消息,我无法在上面post。

Test File

Error Messages

您的头文件 BuckysClass.h 没有包含 <string> 头文件。

添加:

#include<string>
using namespace std;

您应该从相应的 .cpp 文件中删除这些包含,只在 cpp 文件中保留 BuckysClass.h。

语法错误:标识符'string'

#include <string>//include the string header
class BuckysClass {
public:
  BuckysClass();
  BuckysClass(std::string);//add the namespace identifier
  void coolSaying();
};