gpt4 book ai didi

c++ - C++ 的头文件和源文件组织

转载 作者:行者123 更新时间:2023-11-28 02:41:43 25 4
gpt4 key购买 nike

我知道这似乎是一个常见问题。但是,我似乎遵循了头文件的所有标准实践和指南,除了我认为不会妨碍编译的包含保护。仅当 CustomerInformation.h 包含在包含主要方法的 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,配置:Debug Win32 ------1> CPlusPlusTraining.cpp//主文件1>CPlusPlusTraining.obj : error LNK2019: 未解析的外部符号 "public: __thiscall CustomerInformation::CustomerInformation(void)"(??0CustomerInformation@@QAE@XZ) 在函数 _wmain 中引用1>CPlusPlusTraining.obj:错误 LNK2019:未解析的外部符号“public:char __thiscall CustomerInformation::InformationRequest(void)”(?InformationRequest@CustomerInformation@@QAEDXZ) 在函数 _wmain 中引用1>C:\Users\Gordlo_2\documents\visual studio 2013\Projects\CPlusPlusTraining\Debug\CPlusPlusTraining.exe : fatal error LNK1120: 2 Unresolved external ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

最佳答案

您在没有类声明的情况下声明了 Customer 类的成员函数,导致编译器不知道这是您原型(prototype)化的函数。应该是:

 char CustomerInformation::InformationRequest() {
//your function stuff
}

关于c++ - C++ 的头文件和源文件组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25737748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com