2013-10-02 83 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

相关问题