Первые шаги...

Все о программировании под *nix
Гость

Первые шаги...

Сообщение Гость »

Пытаюсь скомпилить прогу следущего содержания:

#include <iostream.h>
#include <fstream.h>
int main(void)
{
ofstream out("test");

if(!out)
{
cout << "Can't open file";
return 0;
}

out << 10 << " " << 123.23 << "\n";
out << "This is a short text file.\n";

out.close();

return 0;
}

а gcc в ответ:

/home/kir/tmp/ccdmnz8t.o: In function `main':
/home/kir/tmp/ccdmnz8t.o(.text+0x22): undefined reference to `ofstream::ofstream(int, char const *, int, int)'
/home/kir/tmp/ccdmnz8t.o(.text+0x3b): undefined reference to `ios::operator!(void) const'
/home/kir/tmp/ccdmnz8t.o(.text+0x51): undefined reference to `cout'
/home/kir/tmp/ccdmnz8t.o(.text+0x56): undefined reference to `ostream::operator<<(char const *)'
/home/kir/tmp/ccdmnz8t.o(.text+0x6a): undefined reference to `ofstream::~ofstream(void)'
/home/kir/tmp/ccdmnz8t.o(.text+0xaf): undefined reference to `ostream::operator<<(int)'
/home/kir/tmp/ccdmnz8t.o(.text+0xba): undefined reference to `ostream::operator<<(char const *)'
/home/kir/tmp/ccdmnz8t.o(.text+0xc5): undefined reference to `ostream::operator<<(double)'
/home/kir/tmp/ccdmnz8t.o(.text+0xd0): undefined reference to `ostream::operator<<(char const *)'
/home/kir/tmp/ccdmnz8t.o(.text+0xec): undefined reference to `ostream::operator<<(char const *)'
/home/kir/tmp/ccdmnz8t.o(.text+0xfe): undefined reference to `fstreambase::close(void)'
/home/kir/tmp/ccdmnz8t.o(.text+0x112): undefined reference to `ofstream::~ofstream(void)'
/home/kir/tmp/ccdmnz8t.o(.text+0x12d): undefined reference to `ofstream::~ofstream(void)'
collect2: ld returned 1 exit status

Что бы это могло быть?

Anonymous

Сообщение Anonymous »

Попробуй сделать вместе с этим:

#include<iostream> /*ты ипользуешь cout,нужно подключить этот хедер*/
using namespace std;/* ты исп. имена из пространства std которое не подключил*/

Гость

Сообщение Гость »

Потвоему совету поправил:

#include <iostream>
using namespace std;
#include <fstream>

int main(void)
{
ofstream out("test");

if(!out)
{
cout << "Can't open file";
return 0;
}

out << 10 << " " << 123.23 << "\n";
out << "This is a short text file.\n";

out.close();

return 0;
}

Эффект тотже.

Гость

Сообщение Гость »

Ещё раз в предыдущем почему-то не прошёл <iostream>
#include <iostream>
using namespace std;
#include <fstream>

int main(void)
{
ofstream out("test");

if(!out)
{
cout << "Can't open file";
return 0;
}

out << 10 << " " << 123.23 << "\n";
out << "This is a short text file.\n";

out.close();

return 0;
}

Anonymous

Сообщение Anonymous »

using namespace std; должно идти после директив препроцессору( т. е. , в принципе, все директивы препроцессору должны быть в начале файла), иначе - я не уверен, что компилятор поймет следующую дирктиву (у тебя это -- #include <fstream>)

Anonymous

Сообщение Anonymous »

собирай g++
по умолчанию gcc не линкует с плюсовыми библиотеками
(либо пропиши их руками)

Гость

Сообщение Гость »

СПАСИБО!!! :D :D :D Всё работает.

Ответить