【发布时间】:2016-09-17 03:26:55
【问题描述】:
因此,我在 StackOverflow 上进行了广泛的谷歌搜索和搜索,尽管针对这个确切问题有多个答案,但我无法找到解决方案。
我正在尝试在名为 Fpc5.cpp 的外部文件中创建一个测试类
它的内容是:
Fpc5.cpp
#include "stdafx.h"
#include "Fpc5.h";
#include <iostream>
using std::cout;
class Fpc5 {
int bar;
public:
void testMethod();
};
void Fpc5::testMethod() {
cout << "Hey it worked! ";
}
还有我的主 .cpp 文件:
Test.cpp
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
//#include "Fpc5.cpp"
#include "Fpc5.h";
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "Hello" << endl;
Fpc5 testObj;
testObj.testMethod();
system("pause");
return 0;
}
我读过的所有答案都表明这是因为我曾经将类包含在主文件本身中,这就是我创建头文件的原因
Fpc5.h
#pragma once
void testMethod();
这改变了错误,但仍然没有解决问题。目前我的Test.cpp 无法识别 Fpc5 类。我还尝试在stdafx.h 中添加Fpc5.cpp 和Fpc5.h,但这仍然不能解决问题。
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//#include "Fpc5.cpp"
#include "Fpc5.h"
我确定这是一个简单的语法/概念理解错误,但我对 c++ 很陌生,不确定哪里出了问题。
【问题讨论】:
-
你的类定义需要在头文件中,而不是源文件中。
标签: c++ file class oop compiler-errors