2013-10-02 85 views
0

所以我碰到的這個編譯錯誤,當我把以下內容:用於建築x86_64的參考鏈接錯誤未定義的符號

的bash-3.2 $ G ++ -oa *的.cpp

用於建築x86_64的未定義符號: 「set_function :: set_function(INT)」,從引用:在ccezs7Gk.o _main LD:符號(S)沒有發現建築x86_64的 collect2:LD返回1退出狀態

,但它好像一切在我的文件中是正確的作爲參考。也許我錯過了什麼?

// 
// hw1_sets.cpp 
// 
// 

#include <iostream> 
#include <vector> 
#include "hw1.h" 

using namespace std; 

void set_function::assign()          //Assign function 
{ 
    cin >> set_function::sets; 
    cout << set_function::sets << endl; 
    int i; 
    if(sets == "R")         //if user inputs R 
    { 

     for(i=0; i<13; i++)          //for loop inputting number into R set 
     { 
      cin >> R[i]; 
//   return R[i]; 
     } 

    } 
    else if (sets == "S")        //if user inputs S 
    { 
     for(i=0; i<13; i++)          //for loop inputting number into S set 
     { 
      cin >> S[i]; 
//   return S[i]; 
     } 
    } 
    else if (sets == "T")           //if user inputs T 
    { 
     for(i=0; i<13; i++)          //for loop inputting number into T set 
     { 
      cin >> T[i]; 
//   return T[i]; 
     } 
    } 
    else 
    { 
     cout << "This set does not exist! Try again!" << endl; 
    } 

    cout << " set complete" << endl; 
    }; 

void set_function::clear()           //Clear function 
{ 
    //find set 

    /*cin >> set_function::set; 
    cout << set_function::set << endl; 
    int i; 
    if(set == set_function::R) 
    { 
     for(i=0; i<13; i++) 
     { 
      //clear R values 
     } 
    } 
    else if (set == set_function.S) 
    { 
     for(i=0; i<13; i++) 
     { 
      //clear S values 
     } 
    } 
    else if (set == T) 
    { 
     for(i=0; i<13; i++) 
     { 
      //clear T values 
     } 
    } 

    //remove all values*/ 
} 

void set_function::unionn()           //Union function 
{ 
    //for loop from 0 to 12 (through all elements) 
    //if set1[x] or set2[x] = 1 
     //solution[x]=1 
    //else 
     //solution[x]=0 

} 
void set_function::intersection()         //Intersection function 
{ 
    //for loop from 0 to 12 (through all elements) 
    //if set1[x] == set2[x] 
     //solution[x]=set1[x] 
    //else 
     //solution[x]=0 
} 

void set_function::difference()          //difference function 
{ 
    //for loop from 0 to 12 (through all elements) 
     //set1[x]-set2[x]=solution[x] 
} 



/*printing the set doesn't work*/ 
void set_function::print()           //print function 
{ 
    /*string setname; 
    cin >> setname; 
    if (setname = "R") 
    { 
     for(int i = 0; i<13; i++) 
     { 
      cout<< R[i] << "-"; 
     } 
     cout << endl; 
    } 
    else if (setname = "S") 
    { 
     for(int i = 0; i<13; i++) 
     { 
      cout<< S[i] << "-"; 
     } 
     cout << endl; 
    } 
    else if (setname = "T") 
    { 
     for(int i = 0; i<13; i++) 
     { 
      cout<< T[i] << "-"; 
     } 
     cout << endl; 
    } 

    //else if lastdigit 
     //end of command or newline*/ 
} 

//exit 

// 
// hw1.cpp 
// 
// 
// 
// 

#include "hw1.h" 
#include <iostream> 


using namespace std; 

int main() 
{ 
    string function; 
    set_function sets(27); 

    while(1) 
    { 
     cout << "sets> "; 

     cin >> function; 

     if(function=="assign") 
     { 
      sets->assign(); 
     } 
     else if(function=="clear") 
     { 
      sets->clear(); 
     } 
     else if(function=="union") 
     { 
      sets->unionn(); 
     } 
     else if(function=="intersection") 
     { 
      sets->intersection(); 
     } 
     else if(function=="difference") 
     { 
      sets->difference(); 
     } 
     else if(function=="print") 
     { 
      sets->print(); 
     } 
     else if(function=="quit") 
     { 
//   sets->quit(); 
      return 0; 
     } 

     else 
     { 
      cout<<"error"<<endl; 
     } 
    } 
} 

// 
// hw1.h 
// 
// 
// 
// 

#include <iostream> 

using namespace std; 

class set_function 
{  
    private: 
     bool R[13]; 
     bool S[13]; 
     bool T[13]; 

    public: 
     string sets; 
     int values[13]; 
     /*void r() 
     { 
      R.resize(13); 
     } 
     void s() 
     { 
      S.resize(13); 
     } 
     void t() 
     { 
      T.resize(13); 
     }*/ 
     set_function(int a){} 
     void assign(); 
     void clear(); 
     void unionn(); 
     void intersection(); 
     void difference(); 
     void print(); 
//  void quit(); 

}; 

編輯(13年10月3日@ 12:50P):我改變什麼的評論,現在我得到這個問題:

hw1.cpp: In function ‘int main()’: 
hw1.cpp:28: error: base operand of ‘->’ has non-pointer type ‘set_function’ 
hw1.cpp:32: error: base operand of ‘->’ has non-pointer type ‘set_function’ 
hw1.cpp:36: error: base operand of ‘->’ has non-pointer type ‘set_function’ 
hw1.cpp:40: error: base operand of ‘->’ has non-pointer type ‘set_function’ 
hw1.cpp:44: error: base operand of ‘->’ has non-pointer type ‘set_function’ 
hw1.cpp:48: error: base operand of ‘->’ has non-pointer type ‘set_function’ 

編輯(10/3/13 @ 1:23p):固定。改變了以下內容:正確現在

set_function *sets = new set_function(27) 

set_function *sets; 
sets = new set_function(27); 

編譯。謝謝!

+1

邊注:在您發佈的代碼,您要創建在堆上'sets'(和你忘了在年底將其刪除)。您可以使用'set_function套在堆棧上簡單地創建它(27);' – lolando

+0

ASLO,不要使用這樣的:'具 - > set_function ::分配();'只寫'具 - >分配();' – Roddy

+0

謝謝你們!是的,我原來有sets-> assign(),但我不確定鏈接問題發生在哪裏。哈哈,我會改變set_function設置問題!感謝大家! – thenoelb

回答

3

這不是一個編譯器錯誤,這是一個連接錯誤。你得到它是因爲你爲你的set_function類聲明瞭一個構造函數,但是沒有定義它。

+0

是的,我知道這是一個鏈接器錯誤,使用編譯器,因爲它發生在我試圖編譯。改變了澄清的語法。 我將如何去定義它(可能是一個簡單的答案)?所以,我也與-c標誌編譯它,它編譯罰款,但我不知道如何.o文件結合起來。 – thenoelb

+0

即使嘗試鏈接,即使使用'-c'標誌,它也會失敗。您需要實際編寫一個構造函數,如果你定義一個類中。最簡單的解決方法是用'{}'替換'hw1.h'中'set_function'定義末尾的';'。 –

+0

(刪除評論),因爲我明白你的意思。我改變了它,新的錯誤被列爲上面的編輯 – thenoelb

0

所以你在hw1.h聲明set_function::set_function(int a)。然後main.cpp正確編譯爲調用的set_function構造函數是在頭文件hw1.h發現聲明是正確的。

但是,這個功能是不是在任何地方實現連接發生,並且呼叫沒有解決。

你應該實現它的權利在頭文件或文件hw1.cpp

相關問題