2013-12-18 34 views
1

我不能用gcc編譯了。奇怪的錯誤 - 鏈接錯誤?與海灣合作委員會

使用gcc -o run mytest.cpp,得出:

mytest.cpp:12:9: error: expected ‘;’ at end of member declaration 
mytest.cpp:12:14: error: expected unqualified-id before ‘;’ token 
mytest.cpp: In constructor ‘matrix::matrix()’: 
mytest.cpp:23:26: error: invalid conversion from ‘int**’ to ‘int’ [-fpermissive] 
mytest.cpp:24:24: error: expected ‘;’ before ‘)’ token 
mytest.cpp:25:22: error: invalid types ‘int[int]’ for array subscript 
mytest.cpp:28:13: error: no match for ‘operator~’ in ‘~matrix()’ 
mytest.cpp:28:13: note: candidates are: 
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ios:43:0, 
       from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/ostream:40, 
       from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/iostream:40, 
       from mytest.cpp:1: 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:89:3: note: std::_Ios_Fmtflags std::operator~(std::_Ios_Fmtflags) 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:89:3: note: no known conversion for argument 1 from ‘matrix’ to ‘std::_Ios_Fmtflags’ 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:129:3: note: std::_Ios_Openmode std::operator~(std::_Ios_Openmode) 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:129:3: note: no known conversion for argument 1 from ‘matrix’ to ‘std::_Ios_Openmode’ 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:167:3: note: std::_Ios_Iostate std::operator~(std::_Ios_Iostate) 
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/bits/ios_base.h:167:3: note: no known conversion for argument 1 from ‘matrix’ to ‘std::_Ios_Iostate’ 
mytest.cpp:28:14: error: expected ‘;’ before ‘{’ token 
mytest.cpp: In function ‘int main()’: 
mytest.cpp:45:3: error: ‘class matrix’ has no member named ‘add’ 
mytest.cpp: At global scope: 
mytest.cpp:52:28: error: no ‘matrix matrix::add(matrix)’ member function declared in class ‘matrix’ 

代碼使用:

#include <iostream> 
#include <cmath> 
#include <cstdio> 
#include <cstdlib> 

如果我只使用的iostream編譯一個簡單的Hello World代碼,它給了我:

/tmp/ccGrqvwo.o: In function `main': 
simple.cpp:(.text+0x21): undefined reference to `std::cout' 
simple.cpp:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' 
/tmp/ccGrqvwo.o: In function `__static_initialization_and_destruction_0(int, int)': 
simple.cpp:(.text+0x7c): undefined reference to `std::ios_base::Init::Init()' 
simple.cpp:(.text+0x91): undefined reference to `std::ios_base::Init::~Init()' 
... 

但如果我編譯它與g++ -o run simple.cpp,而不是gcc,它的工作原理!

對第一個代碼做同樣的操作不起作用! (我的意思是使用g ++)

+0

在gcc的情況下,錯誤消息告訴你,你忘記了鏈接'libstdC++'。 g ++自動鏈接到'libstdC++'。 – Ali

+0

至於上半年,還不清楚,沒有代碼,很難說。 – Ali

+0

'gcc'用於編譯C代碼。 'g ++'用於編譯C++代碼。根據需要重複,直到差異變得明顯... – twalberg

回答

1

終於...

我再次完全刪除了gcc,g ++。 我重新安裝了,我符號鏈接gcc和g ++,現在它工作!

0

第一個錯誤在MyTest.cpp的第11行上看起來像無損分號一樣無害。除非你修正了這個錯誤,否則你可能會忽略後面的錯誤,因爲語法錯誤會導致編譯器的解析器找到各種虛假的其他錯誤。

至於鏈接的問題,正如其他人所指出的那樣,g++libstdc++自動鏈接,而你必須指示gcc通過-lstdc++這樣做。

+0

:你好,不幸的是,它不是分號。代碼工作正常。它只是使用'cmath,cstdlib,cstdio'。有些東西打破了這些。 – George

相關問題