| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"James null" |
| Date: |
24 Sep 2005 09:38:46 PM |
| Object: |
unknown compile error |
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'
.
|
|
| User: "Peter_Julian" |
|
| Title: Re: unknown compile error |
25 Sep 2005 12:04:27 AM |
|
|
"James" <null> wrote in message news:43360da2@dnews.tpgi.com.au...
| 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
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: unknown compile error |
24 Sep 2005 09:42:47 PM |
|
|
James wrote:
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
.
|
|
|
|
| User: "jerry.z" |
|
| Title: Re: unknown compile error |
25 Sep 2005 01:01:51 AM |
|
|
i think you must add : using namespace std;
"James" <null> 写入消息新闻:43360da2@dnews.tpgi.com.au...
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'
.
|
|
|
| User: "Peter_Julian" |
|
| Title: Re: unknown compile error |
25 Sep 2005 10:06:19 AM |
|
|
rearranged...
"jerry.z" <mail.zhf@163.com> wrote in message
news:dh5fgh$1rt$1@mail.cn99.com...
Can someone please help me with these errors?
<snip>
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.
.
|
|
|
|
|

|
Related Articles |
|
|