unknown compile error

J

James

Can someone please help me with these errors?

#include <iostream>
#include <string>

class holder
{
private:
int vid;
string vtype;
public:
holder(int id, string str);
~holder();
int getvid();
void setvid(int id);
string gettype();
};



g++ -c -ansi -Wall -pedantic holder.cpp
In file included from holder.cpp:1:
holder.h:8: error: `string' does not name a type
holder.h:8: error: extra semicolon
holder.h:11: error: `string' has not been declared
holder.h:11: error: ISO C++ forbids declaration of `str' with no type
holder.h:15: error: `string' does not name a type
holder.h:15: error: extra semicolon
holder.cpp:3: error: `string' has not been declared
holder.cpp:4: error: ISO C++ forbids declaration of `str' with no type
holder.cpp: In constructor `holder::holder(int, int)':
holder.cpp:6: error: `vtype' undeclared (first use this function)
holder.cpp:6: error: (Each undeclared identifier is reported only once for
each function it appears in.)
holder.cpp: At global scope:
holder.cpp:24: error: `string' does not name a type
*** Error code 1
make: Fatal error: Command failed for target `holder.o'
 
V

Victor Bazarov

James said:
Can someone please help me with these errors?

#include <iostream>
#include <string>

class holder
{
private:
int vid;
string vtype;

std::string vtype;

Read about namespaces.
public:
holder(int id, string str);
~holder();
int getvid();
void setvid(int id);
string gettype();
};

[...]

V
 
P

Peter_Julian

"James" <null> wrote in message | Can someone please help me with these errors?
|
| #include <iostream>
| #include <string>
|
| class holder
| {
| private:
| int vid;
| string vtype;

The compiler generated the precise error associated with the above
declaration. While an integer is an example of a primitive data type, a
std::string is not a primitive nor is string a member of the global
namespace. Like most of the Standard Template Library, the std::string
is grouped in a namespace, the std namespace.

| public:
| holder(int id, string str);
| ~holder();
| int getvid();
| void setvid(int id);
| string gettype();
| };
|
|
|
| g++ -c -ansi -Wall -pedantic holder.cpp
| In file included from holder.cpp:1:
| holder.h:8: error: `string' does not name a type
 
P

Peter_Julian

rearranged...


g++ -c -ansi -Wall -pedantic holder.cpp
In file included from holder.cpp:1:
holder.h:8: error: `string' does not name a type

<snip>

| i think you must add : using namespace std;


No, he must not. The using directive belongs in the implementation /
source only. In a header, the programmer needs to be carefull not to
inject the entire std namespace into his code. The std::string should be
fully qualified during declarations.

So if you break down the header of the class, the implementation of the
class and the source that serves as the entry point you get something
like this:
A.h, A.cpp, test.cpp
___

// A.h - class declaration
#ifndef A_H_
#define A_H_ // include guard

#include <string>

class A
{
std::string m_s;
public:
A(std::string s);
};

#endif // include guard A_H_
___

// A.cpp - class implementation

#include "A.h"
using namespace std; // prefer using std::string;

A::A(string s) : m_s(s)
{
}
___

// test.cpp - main entry point
#include "A.h"

int main()
{
A a("a string");

return 0;
}
___

Now you have a clean, organized, manageable, reusable class. As well,
for the user of the class, its a benefit to read

using std:string;

in the A.cpp implementation instead of

using namespace std;

The reasons are obvious even though this may require several using
directives. A quick scan of the source details what parts of the
namespace can be expected within.

If you still have doubts about this aproach, consider grouping the above
class in your own namespace.

namespace MySpace {
class A
{
};
}

where your implementation will look something like this:

// MySpace_A.cpp - class implementation

#include "A.h"
using MySpace::A;
using std::string;

A::A(string s) : m_s(s)
{
}

Note how the using directives help in the class documentation process.
 

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
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top