2016-03-20 9 views
-2

いくつかのデータ構造(マップ、ベクトル、配列)を含むソースファイルを作成しました。そのヘッダーファイルはメインファイルの#includedです。"if"ステートメントで使用されている別のソースファイルで宣言されているベクタへの未定義参照

主なファイルは次のようになります。私は、データ構造にアクセスした

#include "reachability.h" //Where monkey() and vector<int> int_req are declared 

main() 
{ 
    monkey(int_req); // Calling monkey(int_req) here is OK! Bar is visible 

    ... 

    ifstream fp("foo.txt"); 
    if(fp.is_open()) 
    { 
     std::string line; 
     while(getline(fp,line)) 
     { 
      monkey(int_req); //'int_req' is an undefined reference! 
     } 
    } 
} 

そしてreachability.h

#ifndef REACHABILITY_H 
#define REACHABILITY_H 

extern std::vector<int> int_req; 

void monkey(std::vector<int> feces); 

#endif 

そしてreachability.cc

std::vector<int> int_req; 

void monkey(std::vector<int> thrown_obj) 
{ 
    ... //Iteration and dereferencing of "thrown_obj" 
} 

それはdメインのスコープ内のforループでreachability.ccに記載されており、が問題ありませんでした。しかし、この声明で何かうんざりしていることが起こっている。

コンパイラエラー:

lab1.o: In function `main': 
/home/ubuntu/workspace/ECE597/Lab1/lab1.cc:105: undefined reference to `int_req' 
collect2: error: ld returned 1 exit status 

編集:reachability.ccはcompiliationに含まれている:

elusivetau:~/XXXX/XXXX/XXXX $ g++ lab1.cc parser.cc gate.cc reachability.cc -o run 
/tmp/ccJK4O9q.o: In function `main': 
lab1.cc:(.text+0x489): undefined reference to `int_req' 
collect2: error: ld returned 1 exit status 

編集:このプログラムのためのメイクファイル:それは、あなたが何であれ

all: lab1.o parser.o gate.o reachability.o 
    g++ -g lab1.o parser.o gate.o reachability.o -o run 

lab1.o: lab1.cc 
    g++ -g -c lab1.cc 

parser.o: parser.cc 
    g++ -g -c parser.cc 

gate.o: gate.cc 
    g++ -g -c gate.cc 

reachability.o: reachability.cc 
    g++ -g -c reachability.cc 

clean: 
    rm *o run 
+2

「未定義の参照」と言われているのはリンカーのエラーで、 'if'の中の行だけを参照するものではありません。おそらくあなたは 'src1.cpp'にリンクしていません。おそらく –

+0

。私がコンパイルした文は、編集に添付されています。 – MTV

+2

メッセージから、それがリンカーエラーであることを確認できます( 'ld'はリンカーです)。私はその行に 'Src1.cpp'が表示されていませんが、実際に別の名前を持っているのでしょうか?実際の名前と実際のコードを実際に使用する必要があります。そうしないと、何が起こっているのかを理解できなくなります。 –

答えて

1

私たちに正しい情報を与えていない。

これを追加して、このコードをコンパイルするために非コードを削除しました。出来上がりは、それはまた、リンク:

ます。test.cpp:

#include "reachability.h" //Where monkey() and vector<int> int_req are declared 
#include <string> 
#include <iostream> 
#include <fstream> 

main() 
{ 
    monkey(int_req); // Calling monkey(int_req) here is OK! Bar is visible 

    std::ifstream fp("foo.txt"); 
    if(fp.is_open()) 
    { 
     std::string line; 
     while(getline(fp,line)) 
     { 
      monkey(int_req); //'int_req' is an undefined reference! 
     } 
    } 
} 

reachability.h:

#ifndef REACHABILITY_H 
#define REACHABILITY_H 

#include <vector> 

extern std::vector<int> int_req; 

void monkey(std::vector<int> feces); 

#endif 

reachability.cpp:

#include "reachability.h" 

std::vector<int> int_req; 

void monkey(std::vector<int> thrown_obj) 
{ 
} 

これだけで罰金をコンパイルし、リンクを。 mvce

関連する問題