STL <list> declaration in header file

G

G?nter Omer

Hi there!

I'm just trying to compile a header file (unsing Borland C++ Builder
10)implementing a class, containing the declaration of the STL <list>
but it
refuses to work.

The following errors occure:
"test.h" line 29 type name expected
"test.h" line 29 declaration missing

Interesting is that it works if I copy the declaration into the
main.cpp
file!

Here is the source:

<snip fileName="test.h">
class xy
{
public:
void getI(void);
void setI(int);

private:
int i;
};

class xyy
{
public:
void do_something(void);

private:
list <xy *> myList;
};
</snip>


Thanks in Advance!
Günter
 
R

Rolf Magnus

G?nter Omer said:
Hi there!

I'm just trying to compile a header file (unsing Borland C++ Builder
10)implementing a class, containing the declaration of the STL <list>
but it refuses to work.

Header files aren't supposed to be compiled separately.
The following errors occure:
"test.h" line 29 type name expected
"test.h" line 29 declaration missing

Which line is line 29?
Interesting is that it works if I copy the declaration into the
main.cpp file!

What main.cpp file? Your example doesn't show that file. Without
context, there is nothing wrong with that header file, though I'd
recommend making headers self contained, i.e. add every #include the
header needs. So I'd add:

#include <list>

at the top and replace

list <xy *> myList;

with
 
G

G?nter Omer

Thanks for your fast answer and excuse my uncomplete post!

Here are all three files of the testing program, line
29 is marked:

test.h
----
/*





*/


#ifndef TEST_HEADER
#define TEST_HEADER

class xy
{
public:
void getI(void);
void setI(int);

private:
int i;
};

class xyy
{
public:
void do_something(void);

private:
list <xy *> myList; // -> this is line 29!
};

#endif
----


/*





*/

#include <iostream>
#include "test.h"
using namespace std;

int main(int argc, char *argv[])
{

return 1;
}

----

test.cpp
----
/*





*/

#include <iostream>
#include <list>
#include "test.h"

using namespace std;

void xy::getI(void)
{
cout << i << endl;
}

void xy::setI(int iIN)
{
i = iIN;
}


void xyy::do_something(void)
{
xy *xytemp;
xy *myXY = new xy;
myList.push_front(myXY);

list <xy *>::iterator myIterator;
myIterator = myList.begin();

xytemp = *myIterator;
delete myXY;

}

void xyy::eek:utput(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}


/**/
 
T

Thomas Wintschel

G?nter Omer said:
Thanks for your fast answer and excuse my uncomplete post!

Here are all three files of the testing program, line
29 is marked:

test.h
----
/*





*/


#ifndef TEST_HEADER
#define TEST_HEADER

class xy
{
public:
void getI(void);
void setI(int);

private:
int i;
};

class xyy
{
public:
void do_something(void);

private:
list <xy *> myList; // -> this is line 29!
};

#endif
----


/*





*/

#include <iostream>
#include "test.h"
using namespace std;

int main(int argc, char *argv[])
{

return 1;
}

----

test.cpp
----
/*





*/

#include <iostream>
#include <list>
#include "test.h"

using namespace std;

void xy::getI(void)
{
cout << i << endl;
}

void xy::setI(int iIN)
{
i = iIN;
}


void xyy::do_something(void)
{
xy *xytemp;
xy *myXY = new xy;
myList.push_front(myXY);

list <xy *>::iterator myIterator;
myIterator = myList.begin();

xytemp = *myIterator;
delete myXY;

}

void xyy::eek:utput(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}


/**/

You should add the lines:

#include <list>
using namespace std;

to test.h so that the compiler will know what you mean by 'list'.

You could get away with having these two lines in the source file *before*
including test.h but then you would need them in every source file that
includes test.h which would be ugly, messy and bad.

Tom
 
P

Peter Koch Larsen

Thomas Wintschel said:
G?nter Omer said:
Thanks for your fast answer and excuse my uncomplete post!

Here are all three files of the testing program, line
29 is marked:

test.h
----
/*





*/


#ifndef TEST_HEADER
#define TEST_HEADER

class xy
{
public:
void getI(void);
void setI(int);

private:
int i;
};

class xyy
{
public:
void do_something(void);

private:
list <xy *> myList; // -> this is line 29!
};

#endif
----


/*





*/

#include <iostream>
#include "test.h"
using namespace std;

int main(int argc, char *argv[])
{

return 1;
}

----

test.cpp
----
/*





*/

#include <iostream>
#include <list>
#include "test.h"

using namespace std;

void xy::getI(void)
{
cout << i << endl;
}

void xy::setI(int iIN)
{
i = iIN;
}


void xyy::do_something(void)
{
xy *xytemp;
xy *myXY = new xy;
myList.push_front(myXY);

list <xy *>::iterator myIterator;
myIterator = myList.begin();

xytemp = *myIterator;
delete myXY;

}

void xyy::eek:utput(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}


/**/

You should add the lines:

#include <list>
using namespace std;

to test.h so that the compiler will know what you mean by 'list'.

You could get away with having these two lines in the source file *before*
including test.h but then you would need them in every source file that
includes test.h which would be ugly, messy and bad.

Tom
Never put a using directive in a header-file. This is a potential source of
problems.

Kind regards
Peter Koch Larsen
 
U

Unforgiven

Thomas said:
You should add the lines:

#include <list>
using namespace std;

to test.h so that the compiler will know what you mean by 'list'.

You should just add
#include <list>

Then use std::list.

Don't put 'using' in a header.
 

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

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top