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

P

pmatos

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
 
J

Johan

pmatos said:
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
 
D

Dave

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;'
 
M

Mike Wahler

pmatos said:
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
 
S

Sergei Matusevich

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top