-
Unix problems
Hi everone I am new to C++ programming in Unix and have been getting some strange errors. I can compile this code fine in visual C++ on my windows machine, but when I try to compile on a unix machine I get these errors.
Undifined symbol
first reference in file
folowed by a bunch of
"std::basic_ifstream"
"std::.....
"std:... etc
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main ()
{
int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;
infile.open("a:wordy.txt");
if (infile.fail( ))
{
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
}
any ideas?
-
You know, it would *really* help to know what undefined symbol the compiler is telling you about. Why didn't you post the whole error message ? Anyway, this is not the solution to your problem, but your program will never work because of system("pause"). AH AH ! Caught you. You thought you could always get away using system("pause") ? Nope. Try sticking to something more standard instead of using that function. The problem is that the function is perfectly standard yet none of its arguments are. Use cin.get() instead. It's portable and does more or less the same job.
-
Maybe you tried to compile it with a C compiler rather than a C++ compiler.
Or you don't have a properly installed C++ compiler.
Paste your command line, as well as exact error messages.
Make sure your Unix C++ compiler is installed properly by trying a C++ "hello world" program.
-
The code compiles with some warnings and produces this output:
Code:
09:25 ~> g++ -Wall stupid.cpp
stupid.cpp: In function ‘int main()’:
stupid.cpp:11: warning: unused variable ‘wordcount’
stupid.cpp:12: warning: unused variable ‘letters’
stupid.cpp:13: warning: unused variable ‘average’
stupid.cpp:14: warning: unused variable ‘ch’
09:25 ~> ./a.out
Input file opening failed.
sh: pause: command not found
09:25 ~> touch "a:wordy.txt"
09:25 ~> ./a.out
09:25 ~>
-
hmm i will ask my roomate
-
Code:
[
Undefined first referenced
symbol in file
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const/var/tmp//cc0zbedf.o
std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<<char, std::char_traits<char> >& (*)(std::basic_osstream<char, std::char_traits<char> >&))/var/tmp//cc0zbedf.o
std::cout /var/tmp//cc0zbedf.o
std::ios_base::Init::Init() /var/tmp//cc0zbedf.o
srd::ios_base::Init::Init() /var/tmp//cc0zbedf.o
_gxx_personality_v0 /var/tmp//cc0zbedf.o
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic ostream <char. std::char traits <char> >& ) /var/tmp//cc0zbedf.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
sorry it took so long, cut and paste was not working either.
the command line looks like this
bash-3.00$ gcc workdamnit.cpp
-
It should be gcc is the c-compiler.
-
Code:
infile.open("a:wordy.txt");
That isn't going to work either. Unix doesn't use drive letters.
Try
Code:
infile.open("wordy.txt");
Or
Code:
infile.open("/tmp/wordy.txt");
-
You'd probably want this:
Code:
infile.open("./wordy.txt");
which opens wordy.txt in the current directory; or this
Code:
infile.open("~/wordy.txt");
which opens wordy.txt in the user's home directory (set with the $HOME enviroment variable), which probably looks something like /home/user.
Code:
sh: pause: command not found
UNIX doesn't have a pause command, either. You'd be best off with a standard way to pause the program (like getchar(); see the FAQ). Or better yet, leave it out completely, because you're running it from the command line and so don't have to pause the program to see its output.
-
Thanks for all the help. I got it working now.