【发布时间】:2014-09-09 06:13:39
【问题描述】:
我知道这似乎是一个常见问题。但是,我似乎遵循了头文件的所有标准做法和指南,除了我认为不会妨碍编译的包含守卫。仅当 CustomerInformation.h 包含在包含 main 方法的 CPlusPlusTraining.cpp 中时,才会出现所描述的链接器错误。
这是我用于 C++ 文件组织信息的一个来源:http://www.umich.edu/~eecs381/handouts/CppHeaderFileGuidelines.pdf
这是我的标题
class CustomerInformation {
public: CustomerInformation();
public: char InformationRequest();
};
来源
#include "stdafx.h"
#include <iostream>
#include <string>
#include "CustomerInformation.h"
using namespace std;
char InformationRequest() {
string name;
string lastName;
int age;
cout << "Please input your first and last name then your age: \n";
cin >> name >> lastName >> age;
cout << "Customer Information: " << name + " " << lastName + " " << age << "\n";
char correction;
cout << "Is all of this information correct? If it is Enter 'Y' if not Enter 'N' \n";
cin >> correction;
if (correction == 'N') {
cout << "Please Enter your information again: \n";
InformationRequest();
}
return correction;
}`
并且我包含在 CPlusPlusTraining.cpp 中
#include "stdafx.h"
#include <iostream>
#include <string>
#include "CustomerInformation.h"
using namespace std;
头文件和源文件同名。头文件以 .h 结尾,而源文件以 .cpp 结尾。但是,我在编译时遇到了许多链接器错误。这里有什么问题?我将在另一天保存重复的代码包含。谢谢。
我如何在包含 Main 的文件中调用方法
CustomerInformation CI = CustomerInformation::CustomerInformation();
//information request
CI.InformationRequest(); //Not type safe for input
错误详情:
构建开始:项目:CPlusPlusTraining,配置:调试 Win32 ------ 1> CPlusPlusTraining.cpp //带有Main的文件 1>CPlusPlusTraining.obj : 错误 LNK2019: 函数 _wmain 中引用的无法解析的外部符号“public: __thiscall CustomerInformation::CustomerInformation(void)” (??0CustomerInformation@@QAE@XZ) 1>CPlusPlusTraining.obj : 错误 LNK2019: 函数 _wmain 中引用的未解析外部符号“public: char __thiscall CustomerInformation::InformationRequest(void)” (?InformationRequest@CustomerInformation@@QAEDXZ) 1>C:\Users\Gordlo_2\documents\visual studio 2013\Projects\CPlusPlusTraining\Debug\CPlusPlusTraining.exe : 致命错误 LNK1120: 2 unresolved externals ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
【问题讨论】:
-
PS 这不是家庭作业,我只是通过 Bjarne Stroustrup 的一本书来了解 C++ 的基础知识。
-
什么是链接器错误?您是否使用 cpp 文件进行构建,即:
g++ main.cpp InfoRequest.cpp -
查看我的编辑以获取有关调用和错误的详细信息。
-
这里有一些答案:stackoverflow.com/questions/9928238/… 看来您的链接阶段没有包括
cpp。