C++接口(标头)和实现文件不起作用
C++ Interface (Header) and Implementation File Won't Work
我正在上C 类,由于某些原因,我无法将标题文件中的类在我的一个程序上工作。我正在使用Visual Studio 2017,并且通过在Visual Studio中使用Solution Explorer。
我的程序中有两个构造函数,一个默认值,一个没有。我尝试从每个文件中删除第二个文件,然后程序运行,但我无法使其工作。
标题文件:
#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;
}
语法错误:标识符'字符串'
#include <string>//include the string header
class BuckysClass {
public:
BuckysClass();
BuckysClass(std::string);//add the namespace identifier
void coolSaying();
};
您的标题文件buckysclass.h不包括<string>
标头文件。
add:
#include<string>
using namespace std;
您应该从相应的.cpp文件中删除这些内容,然后将BuckySclass.h保留在CPP文件中。
这是测试文件的更多图片,我收到的错误消息我无法发布。
测试文件
错误消息
相关文章:
- 在实现文件中使用头文件的通用 lambda
- 如何使用命令提示符、记事本和 MinGW 使用主文件、头文件和实现文件编译C++程序?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 在实现文件中使用模板参数声明方法
- 模板实现文件中的匿名命名空间
- 创建单独的实现文件和头文件
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 在实现文件中,我们应该更喜欢"using namespace"指令还是将实现包装在命名空间 { } 中?
- 包含适当的标头时,实现文件中的多个定义出错
- 如何在结构中实现文件读取和创建
- 在头文件和实现文件中创建模板模板函数
- C++头/实现文件中的默认和重载构造函数?
- 如何在C++实现文件中实例化类?
- 实现文件只能识别其他类的远期声明
- C++ 将字符串值传递到实现文件时出现问题
- 创建头文件和实现文件时出现问题
- C 从实现文件中访问私有静态成员
- Pimpl习惯用法、单独的接口/实现文件和多个虚拟继承.如何
- 没有实现文件(.cpp)的派生类
- 在标题或实现文件中定义常数的优点