2015-07-01 22 views
-3

我有3个类的程序,当我尝试在主要的OOP中出现错误时。我尝试改变堆栈中其他帖子的gcc命令,但错误不会改变。我错过了什么?在eclipse中未定义对'main'C++错误的引用

我的项目名称是temp,并且有Region,Sensor和Network类。 我试试这个命令gcc Region_temp.c -o Region_temp

/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2 
make: *** [temp] Error 1 
undefined reference to main 

main.cpp中:

#include <iostream> 
#include "Region.h" 
using namespace std; 
namespace personalization { 
    int main() { 

     int t ; 
     for (cin >> t ; t != 0; t--){  // reads t different test cases . 

      Region region ; 

      while(true) 
      { 
       int sn , temp; 
       cin >> sn >> temp; 
       if (sn == 0 && temp == 0) 
        break; 
       region.add_sensor(sn , temp) ; 
      } 
      while (true) 
      { 
       int sn1 , sn2 ; 
       cin >> sn1 >> sn2 ; 
       if (sn1 == 0 && sn2 == 0) 
        break; 
       if (! (region . same_network(sn1 , sn2))) 
        region.join_networks (sn1 , sn2) ; 
      } 
      cout << region . num_of_networks () << " " << region . max_avg_temp () << endl ; 
     } 

     return 0; 
    } 
} 
+0

您能提供代码示例吗? –

+0

@AndreasDM我附加他们在我的问题... – girl71

回答

4

您不应该将main函数放在名称空间中。如果你这样做的话,编译器(链接器)找不到它。从命名空间中删除它。

int main() { 

     int t ; 
     for (cin >> t ; t != 0; t--){  // reads t different test cases . 

      Region region ; 

      while(true) 
      { 
       int sn , temp; 
       cin >> sn >> temp; 
       if (sn == 0 && temp == 0) 
        break; 
       region.add_sensor(sn , temp) ; 
      } 
      while (true) 
      { 
       int sn1 , sn2 ; 
       cin >> sn1 >> sn2 ; 
       if (sn1 == 0 && sn2 == 0) 
        break; 
       if (! (region . same_network(sn1 , sn2))) 
        region.join_networks (sn1 , sn2) ; 
      } 
      cout << region . num_of_networks () << " " << region . max_avg_temp () << endl ; 
     } 

     return 0; 
    } 
+1

你还需要添加主要“使用命名空间个性化;” –

+0

当我不使用'namespace std;' – girl71

+0

@ girl71时,我的错误没有改变,Chol Nhial在他的第一条评论中提到了这个问题。我建议编辑答案,正式添加该笔记。 – user4581301

2

gcc Region_temp.c -o Region_temp的 “O” 将导致编译找一个main()在Region_temp.c,并设法使一个名为Region_temp

二进制尝试gcc -c Region_temp.c后面跟g++ Region_temp.o main.cpp -o Region_temp

当你在它的时候,你可能想要将C++文件重命名为.cpp并使用g ++编译它们

编辑:现在代码示例已发布,@Chol Nhial已正确地观察到,您也命名空间 - 您的main()...不这样做。

+0

对不起,我迷惑,在图片中,我使用这个命令,你说 – girl71

+0

@ girl71这些命令行命令将在终端中使用(而不是在eclipse中运行)。只需在终端上运行它们,无论如何你都会学到更多的东西。 – technosaurus

+0

tnx,是的,我是新的c + +和Linux – girl71