- 论坛徽章:
- 0
|
按照教材上的代码写了一个例子程序
/home/lyc/c++]more test1.cpp
#include <iostream>;
using namespace std
void read() { cout << "read()\n"; };
void sort() { cout << "sort()\n"; };
void compact() { cout << "compact()\n"; };
void write() { cout << "write()\n"; };
int main()
{
read();
sort();
compact();
write();
return 0;
}
可是编译出现好多错误,查了一些资料,好像没有好的
解决办法,也许因为这个太初级了吧,能帮忙看看吗?
/home/lyc/c++]gcc test1.cpp
test1.cpp:3: parse error before `void'
test1.cpp:4: `void' fails to be a typedef or built in type
test1.cpp:4: ISO C++ forbids declaration of `sort' with no type
test1.cpp: In function `int sort()':
test1.cpp:4: `cout' undeclared (first use this function)
test1.cpp:4: (Each undeclared identifier is reported only once for each
function it appears in.)
test1.cpp: At global scope:
test1.cpp:5: `void' fails to be a typedef or built in type
test1.cpp:5: ISO C++ forbids declaration of `compact' with no type
test1.cpp:6: `void' fails to be a typedef or built in type
test1.cpp:6: ISO C++ forbids declaration of `write' with no type
test1.cpp: In function `int main()':
test1.cpp:9: `read' undeclared (first use this function)
test1.cpp:10: `sort' undeclared (first use this function)
test1.cpp:11: `compact' undeclared (first use this function)
test1.cpp:12: `write' undeclared (first use this function)
/usr/include/c++/3.2/bits/stl_algobase.h: In function `const _Tp&
std::min(const _Tp&, const _Tp& [with _Tp = size_t]':
/usr/include/c++/3.2/bits/stl_algobase.h:793: instantiated from here
/usr/include/c++/3.2/bits/stl_algobase.h:153: `__a' undeclared (first use this
function)
/usr/include/c++/3.2/bits/stl_algobase.h:153: `__b' undeclared (first use this
function)
/usr/include/c++/3.2/bits/stl_algobase.h: In function `bool
std::lexicographical_compare(_InputIter1, _InputIter1, _InputIter2,
_InputIter2) [with _InputIter1 = const signed char*, _InputIter2 = const
signed char*]':
/usr/include/c++/3.2/bits/stl_algobase.h:805: instantiated from here
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__last2' undeclared (first use
this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__first2' undeclared (first use
this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__last1' undeclared (first use
this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__first1' undeclared (first use
this function)
/usr/include/c++/3.2/cmath: In function `_Tp std::__pow_helper(_Tp, int) [with
_Tp = double]':
/usr/include/c++/3.2/cmath:419: instantiated from here
/usr/include/c++/3.2/cmath:392: `__x' undeclared (first use this function)
/usr/include/c++/3.2/cmath:392: `__n' undeclared (first use this function)
/usr/include/c++/3.2/cmath:392: `__cmath_power' undeclared (first use this
function)
然后修改代码为
/home/lyc/c++]more test1.cpp
#include <iostream>;
using namespace std ;
void read() { cout << "read()\n"; };
void sort() { cout << "sort()\n"; };
void compact() { cout << "compact()\n"; };
void write() { cout << "write()\n"; };
int main()
{
read();
sort();
compact();
write();
return 0;
}
加了一个分号,错误改变为
/home/lyc/c++]gcc test1.cpp
/tmp/ccjsGokZ.o: In function `read()':
/tmp/ccjsGokZ.o(.text+0xf): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `sort()':
/tmp/ccjsGokZ.o(.text+0x2d): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x32): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `compact()':
/tmp/ccjsGokZ.o(.text+0x4b): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x50): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `write()':
/tmp/ccjsGokZ.o(.text+0x69): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x6e): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `__static_initialization_and_destruction_0(int, int)':
/tmp/ccjsGokZ.o(.text+0xc2): undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccjsGokZ.o: In function `__tcf_0':
/tmp/ccjsGokZ.o(.text+0xf1): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccjsGokZ.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
错在哪里呢?能否指点一二,谢谢 |
|