当我试图创建一个对象时,我在Visual Studio中得到了一个LNK2001错误,我认为这是构造函数的问题,因为更改构造函数会改变错误。
Customer bob("Bob", "25 Bob Lane", "01bob82", "M", "bob/bob/bob");这一行给出了这个错误:
Error 1 error LNK2001: unresolved external symbol "public: __thiscall
Customer::Customer(class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >)" (??0Customer@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@0000@Z) D:\Dropbox\Work\C++\C++ Assignment\C++
Assignment\driver.obj包含构造函数的Customer类:
#pragma once
#include "l_list.h"
#include "Account.h"
#include <string>
using namespace std;
class Customer
{
private:
l_list<Account> accounts;
string name;
string address;
string telNo;
string sex;
string dob;
public:
Customer(string name, string address, string telNo, string sex, string dob)
{
Customer::name = name;
Customer::address = address;
Customer::telNo = telNo;
Customer::sex = sex;
Customer::dob = dob;
}
void createAccount()
{
cout << "What type of account?";
}
};发布于 2012-11-30 09:22:28
如果你有链接错误,那么在语法上你的代码是正常的,否则你会得到编译器错误。
您应该检查(或添加)的是使用Customer类的项目的Dependencies属性中的路径。在VS中,你可以找到“项目属性->配置属性->链接器->输入->附加依赖项”。链接器似乎无法找到具有客户实现的外部库。你可以成功地编译你的项目,因为所有的#include都是正确的,但是你在链接阶段失败了,只是因为依赖关系。
发布于 2012-11-30 04:42:42
在我看来,那里的东西还可以。检查其他方面,比如确保您的命名空间是正确的,或者没有另一个/冲突的“客户”定义,等等。尝试注释掉大段代码或将代码缩减为一个小的测试用例。
发布于 2014-09-17 23:24:27
我遇到了完全相同的问题。下面是我修复的方法:
在调用Customer构造函数的文件中使用#include<string>而不是#include "string.h"。
https://stackoverflow.com/questions/13634193
复制相似问题