This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
                                
                                    (32个答案)
                                
                        
                                4年前关闭。
            
                    
我认为我的问题来自汽车课;但是我找不到它,请帮助我。

#include <iostream>
void main()
{
    int n = rand() % 6;
    int time = 1;
    for (int i = 0; i < n; i++) {
        Car<int> car1; // this place is error
        // BianDao.QInsert(car);
        int n = 0;
        car1.Get_Car_Number(n);
        cout << n << endl;
    }
}

template <class T>
class Car {
    int Car_Number;
    int Start_Time;
    int Out_Time;

public:
    Car(const T &item = 0);
    ~Car();
    bool Get_Car_Number(T &item)
    {
        item = Car_Number;
        return true;
    }
    bool Get_Start_Time(T &item);
    bool Get_Out_Time(T &item);
    bool Set_Start_Time(const T &item);
};

template <class T>
Car<T>::Car(const T &item)
    : Start_Time(item)
{
    Car_Number = 9999 + rand() % 99999;
}


错误:

error LNK2019: 无法解析的外部符号 "public: __thiscall
Car<int>::~Car<int>(void)" (??1?$Car@H@@QAE@XZ),该符号在函数 _main
中被引用  C:\Users\Administrator\documents\visual studio
2013\Projects\停车场管理系统\停车场管理系统\main.obj 停车场管理系统

最佳答案

现在它正在工作:

#include <iostream>
template<typename T>  // was missing
class Car{
int Car_Number;
int Start_Time;
int Out_Time;
public:
    Car(const T&item = 0);
    ~Car();
    bool Get_Car_Number(T&item){
        item = Car_Number; return true;
    }
    bool Get_Start_Time(T&item);
    bool Get_Out_Time(T&item);
    bool Set_Start_Time(const T&item);
};
template<typename T>    // was missing
Car<T>::Car(const T&item):Start_Time(item){//this is car's constructor
Car_Number = 9999 + rand() % 99999;
}


您忘记将template<typename T>放在类及其构造函数之前。
检查:http://ideone.com/YZN2EA

08-27 14:21