2013-10-29 5 views
18

मैं अंततः बहुत हताश हूं। तो, मेरे सी ++ वर्ग में हमें कक्षाओं का उपयोग करने का निर्देश दिया गया था। हमारे पास हेडर फ़ाइल क्लास और फंक्शंस घोषित करेगी जबकि एक अलग .cpp फ़ाइल इसे कार्यान्वित करेगी। चीजें काम करनी चाहिए, लेकिन वे नहीं हैं और वेब पर कोई समाधान मेरे लिए काम नहीं कर रहा है। मैं इसके लिए लिनक्स पर जी ++ कंपाइलर का उपयोग कर रहा हूं, और यह आईडीई या सामान्य कमांड लाइन पर काम नहीं कर रहा है।जी ++ वर्ग के लिए अनिर्धारित संदर्भ :: फ़ंक्शन

त्रुटि मैं अपने TBook.h में हो रही है यह है:

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
/tmp/ccxqI6An.o: In function `TBook::TBook()': 
TBook.cpp:(.text+0x3b): undefined reference to `Telephone::Telephone()' 
TBook.cpp:(.text+0x100): undefined reference to `Telephone::Telephone()' 
TBook.cpp:(.text+0x132): undefined reference to `Telephone::allNum(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
TBook.cpp:(.text+0x182): undefined reference to `Telephone::~Telephone()' 
TBook.cpp:(.text+0x191): undefined reference to `Telephone::~Telephone()' 
TBook.cpp:(.text+0x2b3): undefined reference to `Telephone::~Telephone()' 
TBook.cpp:(.text+0x2e6): undefined reference to `Telephone::~Telephone()' 
TBook.cpp:(.text+0x2fa): undefined reference to `Telephone::~Telephone()' 
/tmp/ccxqI6An.o:TBook.cpp:(.text+0x370): more undefined references to `Telephone::~Telephone()' follow 
/tmp/ccxqI6An.o: In function `TBook::write()': 
TBook.cpp:(.text+0x4e1): undefined reference to `Telephone::getNumber()' 
TBook.cpp:(.text+0x506): undefined reference to `Telephone::getAreaCode()' 
TBook.cpp:(.text+0x53a): undefined reference to `Telephone::getName()' 
/tmp/ccxqI6An.o: In function `TBook::lookup(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
TBook.cpp:(.text+0x6d4): undefined reference to `Telephone::getName()' 
TBook.cpp:(.text+0x79e): undefined reference to `Telephone::Telephone(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
/tmp/ccxqI6An.o: In function `TBook::print()': 
TBook.cpp:(.text+0x880): undefined reference to `Telephone::getName()' 
TBook.cpp:(.text+0x8e0): undefined reference to `Telephone::getNumber()' 
TBook.cpp:(.text+0x8ff): undefined reference to `Telephone::getAreaCode()' 
collect2: ld returned 1 exit status 
[Finished in 0.3s with exit code 1] 

मैं एक तरह से पसंद नहीं कर रहा हूँ कि फ़ाइल टेलीफोन वर्ग के तरीकों में से किसी को प्राप्त नहीं है। यहाँ कोड TBook.h के लिए कैसा दिखता है:

#ifndef TBOOK_H 
#define TBOOK_H 

#include "Telephone.h" 

class TBook{ 
private: 
    Telephone rolodex[10]; 
    int current; 
    int max; 
public: 
    TBook(); 
    ~TBook(); 
    void add(Telephone); 
    void write(); 
    bool is_full(); 
    void print(); 
    void check(); 
    Telephone lookup(string); 

}; 

#endif 

और यही TBook.cpp की तरह लग रहा है:

#include <iostream> 
#include <fstream> 
#include <string> 

#include "TBook.h" 
#include "Telephone.h" 


using namespace std; 

TBook::TBook(){ 
    current = 0; 
    max = 9; 

    cout << "Hello" << endl; 

    string line; 
    ifstream myfile ("rolodex.txt"); 
    if (myfile.is_open()){ 
     while (getline (myfile,line)){ 
      cout << line << endl; 
      Telephone t; 
      t.allNum(line); 
      add(t); 

     } 
     myfile.close(); 
    }else if (!myfile.is_open()){ 
     ofstream myfile; 
     myfile.open ("rolodex.txt"); 
     myfile << "This is an empty file (Relatively)."; 
     myfile.close(); 
    } 
} 

TBook::~TBook(){ 

} 

void TBook::add(Telephone tel){ 
    if (!is_full()){ 
     rolodex[current] = tel; 
     current++; 
    } 
} 

void TBook::write(){ 
    ofstream myfile; 
    myfile.open ("rolodex.txt"); 
    for (int i = 0; i < current; ++i) 
    { 
     myfile << rolodex[i].getName() << "," << rolodex[i].getAreaCode() << "," << rolodex[i].getNumber() << "\n"; 
    } 
    myfile.close(); 
} 

bool TBook::is_full(){ 
    if (current <= max){ 
     return false; 
    } 
    return true; 
} 

Telephone TBook::lookup(string lookName){ 
    for (int i = 0; i < current; ++i){ 
     if (rolodex[i].getName() == lookName){ 
      return rolodex[i]; 
     } 
    } 
    return Telephone(100, "", "1000000"); 
} 

void TBook::print(){ 
    //Print the vairables 
    for (int i = 0; i < current; ++i){ 
     cout << "Name: " << rolodex[i].getName() << endl; 
    cout << "Number: (" << rolodex[i].getAreaCode() << ") " << rolodex[i].getNumber() << endl; 
    } 

} 

void TBook::check(){ 
    cout << "the message" << endl; 
} 

के बाद से समस्या टेलीफोन वर्ग के साथ उत्पन्न होने वाली प्रतीत हो रहा है, मुझे लगता है मैं समझ यह भी कहा कि कोड दिखाएगी भी

Telephone.h

..

#ifndef TELEPHONE_H 
#define TELEPHONE_H 

#include <iostream> 
#include <string> 


using std::string; 

class Telephone{ 
private: 
    string name; 
    string num; 
    int areaCode; 
public: 
    Telephone(int, string, string); 
    Telephone(); 
    ~Telephone(); 
    bool setAreaCode(int); 

    //Setters 

    void setName(string); 
    void setNumber(string); 
    bool allNum(string); 

    //Getters 
    string getName(); 
    string getNumber(); 
    int getAreaCode(); 

    //Checks 
    bool checkX(int); 
    bool checkY(int); 


}; 

#endif 

Telephone.cpp

..

#include <iostream> 
#include <string> 
#include <stdio.h> 
#include <stdlib.h> 

#include "Telephone.h" 

using namespace std; 

Telephone::Telephone(){ 
    areaCode = 0 
    name = ""; 
    num = ""; 
} 

Telephone::Telephone(int aCode, string nam, string number){ 
    areaCode = aCode; 
    name = name; 

} 

Telephone::~Telephone(){ 
    //Nope Nada 
} 

bool Telephone::allNum(string all){ 
    size_t found = all.find_first_of(","); 

    // 
    string first = all.substr(0, found); 
    string second = all.substr((found)+1, found+1); 
    string third = all.substr(all.find_last_of(",")+1, all.length()); 
    int x, y; 
    //convert string to int values 
    if(third.length() == 7){ 
     x = atoi(third.substr(0,3).c_str()), 
     y = atoi(third.substr(3,4).c_str()); 
    }else{ 
     cerr << "Your phone number is not valid" << endl; 
    } 
    int ac = atoi(second.substr(0, second.length()).c_str()); 

    setName(first); 
    if (!setAreaCode(ac)){ 
     setAreaCode(100); 
     return true; 

    } 
    if (!checkX(x) || !checkY(y)){ 
      setNumber("1000000"); 
     }else{ 
      setNumber(third); 
    } 

    cerr << "The info provided is not valid" << endl; 
    return false; 
} 

void Telephone::setNumber(string number){ 
    num = number; 
} 

bool Telephone::setAreaCode(int aCode){ 
    if(aCode >= 100 && aCode <= 999){ 
     areaCode = aCode; 
     return true; 
    } 
    return false; 
} 

void Telephone::setName(string theName){ 
    name = theName; 
} 

bool Telephone::checkX(int x){ 
    if(x >= 100 && x <= 999){ 
     return true; 
    } 
    cerr << "First three digits are not valid" << endl; 
    return false; 
} 

bool Telephone::checkY(int y){ 
    if(y >= 0000 && y <= 9999){ 
     return true; 
    } 
    cerr << "Last four digits are not valid" << endl; 
    return false; 
} 



//Getters 
string Telephone::getName(){ 
    return name; 
} 

string Telephone::getNumber(){ 
    return num; 
} 

int Telephone::getAreaCode(){ 
    return areaCode; 
} 

और मेरे मुख्य फ़ाइल (भी बुलाया test.cpp) इस तरह दिखता है:

परीक्षण। सीपीपी

#include <iostream> 
#include <string> 
#include "TBook.h" 
#include "Telephone.h" 

using namespace std; 


int main(int argc, char const *argv[]) 
{ 
    //Create a Rolodex 
    TBook addressBook; 

    return 0; 
} 

मैं भी test.cpp के साथ इस त्रुटि मिल रही है

/tmp/ccl8anRb.o: In function `main': 
test.cpp:(.text+0x24): undefined reference to `TBook::TBook()' 
test.cpp:(.text+0x38): undefined reference to `TBook::~TBook()' 
collect2: ld returned 1 exit status 

मुझे लगता है कि यह ज्यादातर एक संकलन त्रुटि है, लेकिन मैं अभी भी यकीन नहीं है, और मुझे लगता है कि मैं कर रहा हूँ मेमे का सेटअप "मेरा कोड काम नहीं करता है, और मुझे नहीं पता क्यों।" आम तौर पर मैं कोड को बाश करता हूं और विभिन्न तरीकों का प्रयास करता हूं जब तक यह काम नहीं करता है, लेकिन मेरे पास बस समय नहीं है। इसलिए मुझे आपकी मदद चाहिए।

+0

, मैं हेडर फाइल में गार्ड का प्रयोग किया: सभी कदम संकलक आपके लिए कैसा प्रदर्शन देखने के लिए, एक -v में फेंक देते हैं। वे वास्तव में मेरे लिए काम नहीं करते हैं। – Kivo360

+7

मुझे इसमें अधिक दिलचस्पी है: 1) जहां आपकी फाइलें हैं (पूरी तरह से उसी डीआईआर में?) और 2) कमांड लाइन जो आप अपने कोड को संकलित करने के लिए उपयोग करते हैं। क्या आप '-c' का उपयोग कर रहे हैं, फिर लिंक कर रहे हैं? अन्यथा, क्या आप सभी कक्षाओं वाली सभी फाइलों को 'g ++' दे रहे हैं? बेवकूफ और आसान तरीका: 'g ++ * .cpp' ... – ShinTakezou

+0

क्या ShinTakezou कहते हैं; त्रुटि प्रतीत होती है जैसे आप निष्पादन योग्य लिंक करते समय सभी ऑब्जेक्ट फ़ाइलों को लिंक नहीं कर रहे हैं। – Angew

उत्तर

31

यह एक लिंकर त्रुटि है। प्रयास करें:

g++ test.cpp Telephone.cpp -o test 

असल में, लिंकर कार्यों आप थे, लेकिन के लिए एक कार्यान्वयन नहीं प्रदान की थी के बारे में शिकायत कर रहा है। इसके अलावा

g++ -v test.cpp Telephone.cpp -o test 
संबंधित मुद्दे