2012-03-09 183 views
2

我想創建一個類A的對象。編譯工作正常,但鏈接器抱怨LNK2019無法解析的外部符號D :: D函數A :: A中引用。不能解決無法解決的外部錯誤LNK2019錯誤

A.cpp

#include "../A.hpp" 

using namespace <name>; 

A::A(D* low, D* mid, D* high, M* m) 
{ 
    std::vector<B*>* lTC = split(low); 
    std::vector<B*>* mTC = split(mid); 
    std::vector<B*>* hTC = split(high); 

    D* lDC = new D(lTC); 
    D* mDC = new D(mTC); 
    D* hDC = new D(hTC); 

    mr = m; 

    procRDC = new std::vector<D*>(); 
    procRDC->push_back(lDC); 
    procRDC->push_back(mDC); 
    procRDC->push_back(hDC); 
} 

std::vector<B*>* A::split(D* d) 
{ 
    std::vector<B*>* tc = new std::vector<B*>(); 
    std::vector<B*>* ob = d->getB(); 

    for (std::vector<B*>::iterator it = ob->begin(); it != ob->end(); ++it) 
    { 
     B* b= *it; 

     int a1 = b->getA; 
     int a2 = b->getA; 
     int a3 = b->getA; 

     B* b1 = new B(a1, a2, a3); 
     B* b2 = new B(a3, a2, a1); 

     tc ->push_back(b1); 
     tc ->push_back(b2); 
    } 

    return tc; 
} 

A.hpp

#ifndef A_HPP 
#define A_HPP 

#include "../dec.hpp" 
#include "../D.hpp" 
#include "../M.hpp" 
#include "../B.hpp" 
#include <vector> 

using namespace <name>; 

class A: public dec 
{ 
protected: 
    M* mr; 
    std::vector<D*>* procRDC; 

public: 
    A(D* low, D* mid, D* high, M* marketRound); 

protected: 
    std::vector<B*>* split(D* d); 

}; // class A 

#endif 

基類dec.cpp包含一個空的構造 我也給你D.cpp

#include "../D.hpp" 

using namespace <name>; 

D::D(std::vector<B*>* b) 
{ 
    bs = b; 
} 

std::vector<B*>* D::getB() 
{ 
    return bs; 
} 

和d .hpp

#ifndef D_HPP 
#define D_HPP 

#include "../B.hpp" 
#include <vector> 

using namespace <name>; 

class D: public C 
{ 
protected: 
    std::vector<B*>* bs; 

public: 
    D(std::vector<B*>* b); 
    std::vector<B*>* getB(); 

}; // class D 

#endif 

C類只包含一個空的構造

B.cpp

#inlcude "../B.hpp" 

using namespace <name> 

B::B(int a1, int a2, int a3) 
{ 
    a1 = a1; 
    a2 = a2; 
    a3 = a3; 
} 

int B::getA() { return a1; } 

B.hpp

#ifndef B_HPP 
#define B_HPP 

#include "../O" 

using namespace <name> 

class B : public O 
{ 
protected: 
    int a1; 
    int a2; 
    int a3; 

public: 
    B(int a1, int a2, int a3); 

public: 
    int B::getA(); 

}; 

#endif 

現在,它給出了一個錯誤,它無法找到d :: d中引用函數A :: A,以及它在A :: A和D :: D中找不到B :: B的一些錯誤。我已經嘗試添加一個主函數,刪除基類定義,再次檢查包含頭文件......在Visual Studio 10中選擇「跳轉到聲明」或「跳轉到定義」時,它可以找到確切的D: :D功能。我打開頭文件以查看它們是否正在指向正確的文件,並確實如此。

你們有什麼建議在哪裏看下去以消除錯誤?你在某處發現錯誤嗎?我搜查了太久以便自己解決。非常感謝幫助! 順便說一句,我不得不改變類的實際命名,但我檢查了僞名是否正確分配。如果需要其他文件來澄清錯誤,請讓我知道,我會很高興地把它們放在這裏。

+0

哦,廢話,C的頭文件應該在D.hpp中添加。這是在真實的代碼中的情況,但我在這裏意外地將其消除,因此添加C.hpp文件不是解決問題的方法 – Marijn 2012-03-09 19:27:11

回答

0

是否D.cpp明確包含在您的構建中?如果D.cpp丟失,那麼你會得到這個錯誤。

要檢查確定,請在D.hpp添加定義爲d:

class D: public C 
{ 
protected: 
    std::vector<B*>* bs; 

public: 
    D(std::vector<B*>* b) : bs(b) {} 
    std::vector<B*>* getB() { return bs; } 

}; // class D 

如果這能解決鏈接錯誤,你不包括在你的構建D.cpp

+0

我試過了您的建議,但它沒有解決錯誤。 cpp也在源文件中可見。 – Marijn 2012-03-10 17:05:09

+0

你的例子適合我(有一些小調整);我可以實例化A,B和D對象。當您將實際代碼簡化爲樣本時,您必須改變某些內容。當你編輯你的問題時,你提到了更多的錯誤;這些'LNK2019'錯誤嗎? – Fraser 2012-03-11 00:15:31

+0

它們都是LNK2019錯誤,都與B或D類相關。我在這裏沒有詳細說明的唯一的事情是基類O,它不是空的,但包含或多或少與B相同的方法。所以,也許這就是一個繼承問題...讓我試着清空基類,看看它明天是否適用於我。如果是這樣,我會發佈一個比上面更具體的問題。如果沒有,我知道上面的代碼不包含重大錯誤。感謝您的幫助! – Marijn 2012-03-11 09:54:41

相關問題