LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-18-2003, 05:58 AM   #1
cyberswami
Member
 
Registered: Jan 2003
Location: TVM,Kerala,INDIA
Distribution: Redhat 8.0
Posts: 47

Rep: Reputation: 15
C++ problem -


Supposing i have a sample problem as follows :-
#include <string.h>
#include <iostream>
int main()
{
string test;
test= "this is a test";
cout<<name;
return 0;
}
and save it as test.cc

on compiling it the following error occurs :--
test.cc: In function `int main()':
test.cc:5: `string' undeclared (first use this function)
test.cc:5: (Each undeclared identifier is reported only once for each function
it appears in.)
test.cc:5: parse error before `;' token
test.cc:6: `name' undeclared (first use this function)
test.cc:7: `cout' undeclared (first use this function)

but the string.h file is in the /usr/share directory

what could be the problem
pls help
 
Old 02-18-2003, 07:28 AM   #2
zurron
LQ Newbie
 
Registered: Feb 2003
Location: Pereira
Distribution: slackware
Posts: 23

Rep: Reputation: 15
#include <string.h>
#include <iostream> // i am not sure that this library works in linux
int main()
{
string test; //string? c++ isn't php or java you must declare like char *
test= "this is a test";
cout<<name; // cout is on conio.h, it isn't linux works only in windows and what is name?
return 0;
}


try this

#include <stdio.h>
int main()
{
char* test;
test= "this is a test";
puts(test);
return 0;
}


[zurron@crvenkapa zurron]# gcc test.cc -o test.x
[root@crvenkapa root]# ./test.x

Last edited by zurron; 02-18-2003 at 07:34 AM.
 
Old 02-18-2003, 08:36 AM   #3
iceman47
Senior Member
 
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Rep: Reputation: 47
Quote:
Originally posted by zurron
cout<<name; // cout is on conio.h, it isn't linux works only in windows and what is name?
cout is defined in iostream.h and I thought that did work in linux. As I remeber correctly, it's a c++ header file, thus it needs to be compiled as c++.
 
Old 02-18-2003, 08:50 AM   #4
zurron
LQ Newbie
 
Registered: Feb 2003
Location: Pereira
Distribution: slackware
Posts: 23

Rep: Reputation: 15
#include <stdio.h>
#include <iostream.h >
int main()
{
char* test;
test= "this is a test";
cout <<test;
return 0;
}


[root@crvenkapa root]# gcc test.cc -o t.x
test.cc:2:23: iostream.h : No such file or directory
test.cc: In function `int main()':
test.cc:7: `cout' undeclared (first use this function)
test.cc:7: (Each undeclared identifier is reported only once for each function
it appears in.)

iceman47, it does not working for me, so, can you tell me how do you make it works?
 
Old 02-18-2003, 10:32 AM   #5
iceman47
Senior Member
 
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Rep: Reputation: 47
can't check it right now, but try to compile with g++ or check the manpage for gcc and look for an option to compile c++ sources
 
Old 02-18-2003, 02:39 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Re: C++ problem -

Quote:
Originally posted by cyberswami
Supposing i have a sample problem as follows :-
#include <string.h>
To get c++ strings you want to include
Code:
#include <string>
You only included "string" manipulation routines
like strcmp, strcpy, which operate on standard
char* in C.

Quote:
#include <iostream>
int main()
{
string test;
test= "this is a test";
cout<<name;
to cout test you should call it test, too ;)

Quote:
return 0;
}
and save it as test.cc

on compiling it the following error occurs :--
test.cc: In function `int main()':
test.cc:5: `string' undeclared (first use this function)
test.cc:5: (Each undeclared identifier is reported only once for each function
it appears in.)
test.cc:5: parse error before `;' token
test.cc:6: `name' undeclared (first use this function)
test.cc:7: `cout' undeclared (first use this function)
as for cout, use std::cout, or put
Code:
using namespace std;
as the first line in main...

Quote:
but the string.h file is in the /usr/share directory

what could be the problem
pls help
Probably a poorly written book you're trying to
learn C++ from :}

Cheers,
Tink
 
Old 02-18-2003, 04:17 PM   #7
0micron
LQ Newbie
 
Registered: Feb 2003
Posts: 3

Rep: Reputation: 0
Remember #include <string.h> and #include <string> are two different things as some of my predecessors have mentionned.
If you wanna declare a string and not a char*, you need to include <string>

hth.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 08:45 PM


All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration