簡體   English   中英

體系結構x86_64參考鏈接器錯誤的未定義符號

[英]Undefined symbols for architecture x86_64 reference linker error

所以當我輸入以下內容時,我遇到了這個編譯錯誤:

bash-3.2 $ g ++ -oa * .cpp

架構x86_64的未定義符號:“ set_function :: set_function(int)”,引用自:ccezs7Gk.o中的_main ld:架構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();

};

編輯(10/3/13 @ 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);

現在可以正確編譯。 謝謝!

這不是編譯器錯誤,而是鏈接器錯誤。 之所以得到它,是因為您為set_function類聲明了構造函數,但從未定義它。

因此,您在hw1.h聲明set_function::set_function(int a) 然后正確編譯main.cpp因為對set_function構造函數的set_function正確,如在頭文件hw1.h聲明的hw1.h

但是該函數未在任何地方實現,因此會發生鏈接並且無法解決該調用。

您應該在頭文件或hw1.cpp文件中hw1.cpp實現它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM