当我尝试编译示例C++项目时遇到一些问题,我试图读取顺序文件,但是当我编译时遇到一些错误,下面是代码:
// ReadClientFile.cpp
// Lendo e imprimindo um arquivo sequêncial.
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;
#include <fstream> // Fluxo de arquivo
using std::ifstream; // Insere fluxo de arquivo
#include <iomanip>
using std::setw;
using std::setprecision;
#include <string>
using std::string;
#include <cstdlib>
using std::exit; // Sai do protótipo de funcão
void outputLine( int, const string, double ); // Protótipo
int main()
{
// Construtor ifstream, abre o arquivo
ifstream inClientFile( "Clients.dat", ios::in );
// Fecha o programa se ifstream não pode abrir o arquivo
if ( !inClientFile )
{
cerr << "File could not be opened" << endl;
exit ( 1 );
} // Fim do if
int account;
char name[ 30 ];
double balance;
cout << left << setw( 10 ) << "Account" << setw( 13 )
<< "Name" << "Balance" << endl << fixed << showpoint;
// Exibe cada registro do arquivo
while ( inClientFile >> account >> name >> balance )
outputLine( account, name, balance );
return 0; // Destrutor ifstream fecha o arquivo
} // Fim de main
// Exibe um registro do arquivo
void outputLine( int account, const string name, double balance )
{
cout << left << setw( 10 ) << account << setw( 13 ) << name
<< setw( 7 ) << setprecision( 2 ) << right << balance << endl;
} // Fim da funcão outputLine
这是编译日志:
ubuntu@ubuntu-laptop:~/C++$ gcc ReadClientFile.cpp
/tmp/ccVkuUp1.o: In function `__static_initialization_and_destruction_0(int, int)':
ReadClientFile.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
ReadClientFile.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccVkuUp1.o: In function `outputLine(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)':
ReadClientFile.cpp:(.text+0xd1): undefined reference to `std::cout'
ReadClientFile.cpp:(.text+0xd6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0xe7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0xf8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
ReadClientFile.cpp:(.text+0x109): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x11a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
ReadClientFile.cpp:(.text+0x12b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x13c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setprecision)'
ReadClientFile.cpp:(.text+0x14c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x15b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
ReadClientFile.cpp:(.text+0x163): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x16b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccVkuUp1.o: In function `main':
ReadClientFile.cpp:(.text+0x1ac): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
ReadClientFile.cpp:(.text+0x1bf): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator!() const'
ReadClientFile.cpp:(.text+0x1d2): undefined reference to `std::cerr'
ReadClientFile.cpp:(.text+0x1d7): 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*)'
ReadClientFile.cpp:(.text+0x1df): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x1e7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
ReadClientFile.cpp:(.text+0x234): undefined reference to `std::cout'
ReadClientFile.cpp:(.text+0x239): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x24d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x25d): 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*)'
ReadClientFile.cpp:(.text+0x271): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x281): 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*)'
ReadClientFile.cpp:(.text+0x291): 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*)'
ReadClientFile.cpp:(.text+0x299): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x2a1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
ReadClientFile.cpp:(.text+0x2b1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x2c1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x2e0): undefined reference to `std::allocator<char>::allocator()'
ReadClientFile.cpp:(.text+0x2ff): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
ReadClientFile.cpp:(.text+0x32f): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
ReadClientFile.cpp:(.text+0x357): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
ReadClientFile.cpp:(.text+0x373): undefined reference to `std::allocator<char>::~allocator()'
ReadClientFile.cpp:(.text+0x39b): undefined reference to `std::allocator<char>::~allocator()'
ReadClientFile.cpp:(.text+0x3c4): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
ReadClientFile.cpp:(.text+0x3d5): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*)'
ReadClientFile.cpp:(.text+0x3e9): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
ReadClientFile.cpp:(.text+0x3fd): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
ReadClientFile.cpp:(.text+0x41d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
ReadClientFile.cpp:(.text+0x453): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
/tmp/ccVkuUp1.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
最佳答案
可以使用:
g++ ReadClientFile.cpp
或者
gcc ReadClientFile.cpp -lstdc++
我猜gcc默认情况下不链接vs.标准c++库。通常,您应该使用g++编译c++程序。
关于c++ - 在C++中读取顺序文件时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1144253/