对'main'的未定义引用eclipse

时间:2015-07-01 04:01:35

标签: c++ linux gcc

当我尝试使用主要的oop时,我有3个类的程序我有错误。我尝试用堆栈中的其他帖子更改gcc的命令,但错误没有改变。我错过了什么?

我的项目名称是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;
    }
}

2 个答案:

答案 0 :(得分:5)

您不应将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 :(得分:2)

gcc Region_temp.c -o Region_temp“-o”将导致编译器在Region_temp.c中查找main()并尝试创建名为Region_temp的二进制文件

尝试gcc -c Region_temp.c后跟g++ Region_temp.o main.cpp -o Region_temp

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

编辑:现在已经发布了代码示例,@ Chol Nhial已经正确地发现你也命名了你的main()...不要这样做。