473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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'
Sep 25 '05 #1
4 10346
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
Sep 25 '05 #2

"James" <null> wrote in message news:43******@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

Sep 25 '05 #3
i think you must add : using namespace std;
"James" <null> дÈëÏûÏ¢ÐÂÎÅ:43******@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'

Sep 25 '05 #4
rearranged...

"jerry.z" <ma******@163.com> wrote in message
news:dh**********@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.
Sep 25 '05 #5

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

Similar topics

3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
3
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown...
0
by: Davis Ming | last post by:
Hi All, I used Microsoft Liner (7.00.9210) with makefile to compile an existing project. It works well until I add "/PDBSTRIPPED" option in order to get public symbol files. The error message is...
5
by: Lars-Erik Aabech | last post by:
Hi! Guess it's my day again.. Tried to deploy a test release of a new asp.net web today, and got a terrible error. The web is running swell on three development computers, but when it's copied...
3
by: manoj.pattanaik | last post by:
Hi, I am trying to compile following piece of code (bb.cpp) using aCC (HP ANSI C++ B3910B A.03.37) compiler on HP-UX 11.23. It gives error:485 //bb.cpp -- Starts #include <iostream> using...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
3
by: Timbo | last post by:
This is a really weird one and very odd. I have written a web site using VS2005 and .Net 2.0, all works very well when running it locally. However when I publish it to my web server (hosted by...
3
by: Sa¹a Bistroviæ | last post by:
Sa¹a Bistroviæ Antuna Mihanviæa 13 40000 Èakovec Croatia sasa.bistrovic@ck.t-com.hr FPC: Exception : Unknown Run-Time error : 210 Hi, I'm Sa¹a from Croatia.
1
by: eljainc | last post by:
Hello, I have a problem when working with a program that uses XAML in one of the modules. I copied some XAML code from another working project into an existing project, then when trying to...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.