8

当我尝试创建一个对象时,我在 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

包含构造函数的客户类:

#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?";

    }

};
4

4 回答 4

9

如果你有链接错误,那么你的代码在语法上是可以的,否则你会得到编译器错误。

您应该检查(或添加)的是使用 Customer 类的项目的 Dependencies 属性中的路径。在VS中,您可以找到它“项目属性->配置属性->链接器->输入->附加依赖项”。似乎链接器找不到具有客户实现的外部库。您可以成功编译您的项目,因为所有#include 都是正确的,但您在链接阶段失败只是因为依赖关系。

于 2012-11-30T01:22:27.673 回答
2

那里的东西对我来说看起来不错。检查其他事情,例如确保您的命名空间是正确的,或者没有另一个/冲突的“客户”定义等。尝试注释掉大部分代码或将您的代码缩减为一个小测试用例。

于 2012-11-29T20:42:42.070 回答
1

我遇到了完全相同的问题。这就是我修复的方式:

在文件调用构造函数中使用#include<string>而不是。#include "string.h"Customer

于 2014-09-17T15:24:27.413 回答
1

我有同样的错误。事实证明,一个必要的功能被注释掉了。当我取消注释此功能时,错误消失了。

于 2015-12-23T08:55:55.243 回答