By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.