473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A 'almost' three liner is killing me...

Hi all,

I'm not used to programming in C++, in fact I just now started to
program in C++ after a 1 year detour through other exotic languages.
Even though I'm programming side by side with C++ Programming Language
Special Editions some issues raised in my program. I was able to
simplify my program to reproduce the errors. Let me tell you before
hand that this seems almost like the 10.4.6 example in the book cited
above so I can't possibly understand what's wrong.
// stest.h
#include <iostream>
#include <string>

class stest {

string name;

public:
stest(const string& n);

};

// stest.cpp
#include "stest.h"

stest::stest(const string & n)
: name(n) {
cout << "DONE" << nl;
}

Can someone explain me why when I try to compile stest.cpp into object
file with g++, I get:
$ g++ -ansi -Wall -c stest.cpp
In file included from stest.cpp:1:
stest.h:6: error: 'string' is used as a type, but is not defined as a
type.
stest.h:9: error: parse error before `&' token
stest.cpp:3: error: parse error before `&' token

Any suggestions on code style or any other comments are extremely
welcomed.

Cheers,

Paulo Matos

Jul 23 '05 #1
4 1284

"pmatos" <po**@sat.inesc-id.pt> schreef in bericht
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi all,

I'm not used to programming in C++, in fact I just now started to
program in C++ after a 1 year detour through other exotic languages.
Even though I'm programming side by side with C++ Programming Language
Special Editions some issues raised in my program. I was able to
simplify my program to reproduce the errors. Let me tell you before
hand that this seems almost like the 10.4.6 example in the book cited
above so I can't possibly understand what's wrong.
// stest.h
#include <iostream>
#include <string>

class stest {

string name;

public:
stest(const string& n);

};

// stest.cpp
#include "stest.h"

stest::stest(const string & n)
: name(n) {
cout << "DONE" << nl;
}

Can someone explain me why when I try to compile stest.cpp into object
file with g++, I get:
$ g++ -ansi -Wall -c stest.cpp
In file included from stest.cpp:1:
stest.h:6: error: 'string' is used as a type, but is not defined as a
type.
stest.h:9: error: parse error before `&' token
stest.cpp:3: error: parse error before `&' token

Any suggestions on code style or any other comments are extremely
welcomed.

Cheers,

Paulo Matos


Hi,

You must use namespace std after your include files

using namespace std;

Hope this helps

Johan
Jul 23 '05 #2
When you #include <string>, the class is put into the std:: namespace.
All you need to do is either import the entire namespace by putting
"using namespace std;" somewhere in scope before you use string, or
refer to string as std::string.

If you choose to not using namespace std, you'll also need to change
cout << "DONE" << nl; to 'std::cout << "DONE" << std::endl;'

Jul 23 '05 #3

"pmatos" <po**@sat.inesc-id.pt> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi all,

I'm not used to programming in C++, in fact I just now started to
program in C++ after a 1 year detour through other exotic languages.
Even though I'm programming side by side with C++ Programming Language
Special Editions some issues raised in my program. I was able to
simplify my program to reproduce the errors. Let me tell you before
hand that this seems almost like the 10.4.6 example in the book cited
above so I can't possibly understand what's wrong.
// stest.h
#include <iostream>
#include <string>

class stest {

string name;
[snip]
stest.h:6: error: 'string' is used as a type, but is not defined as a
type.
stest.h:9: error: parse error before `&' token
stest.cpp:3: error: parse error before `&' token

Any suggestions on code style or any other comments are extremely
welcomed.


See Section 3.3 in your Stroustrup book.

-Mike
Jul 23 '05 #4
On 2005-02-16, Johan <me@knoware.nl> wrote:
// stest.h
#include <iostream>
#include <string>

class stest {

string name;

public:
stest(const string& n);

};

// stest.cpp
#include "stest.h"

stest::stest(const string & n)
: name(n) {
cout << "DONE" << nl;
}
You must use namespace std after your include files

using namespace std;


That's right, string class belongs to an std namespace.
As a rule, you DON'T want to put "using" statement in your header file
as it can conflict with other headers #included later. Instead, you
explicitly specify std:: wherever needed in the .h file. For the
implementation, i.e. your .cpp files, you may write "using namespace std",
just don't forget to put it AFTER all #include statements.

Just my $0.02.

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: D. Alvarado | last post by:
Hello, Does anyone have a PHP 4 one-liner (or two-liner) for extracing a file from a directory in which I know the word "footer" is guaranteed to be in the file name, I know the precise directory...
6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
8
by: Mantorok Redgormor | last post by:
The only adder I was able to come up with for incrementing by one, uses a for loop. I was trying to do this without using a for loop while emulating i++(it's for obfuscated code) Anyone know...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
2
by: jhhbr549 | last post by:
I need my result to print out only in the box created with a scroll bar. It has to look like this. The Twelve Days of It by John H. Howard On the First day of It My true LOVE of Computers...
3
by: srinivasan srinivas | last post by:
Hi, Do we have python one-liner like perl one-liner 'perl -e'?? Thanks, Srini Unlimited freedom, unlimited storage. Get it now,...
37
by: c.lang.myself | last post by:
Can you determine output of following one liner by visual inspection.. It had won an award in obfuscated C contest..... main() { printf(&unix,(unix)+"fun"-0×60);} You can also list your...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.