By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

STL <list> declaration in header file

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
G?nter Omer wrote:
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

std::list <xy *> myList;
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>


Jul 22 '05 #2

P: n/a
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::output(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}
/**/

----

Tanks!
-GŁnter
Jul 22 '05 #3

P: n/a

"G?nter Omer" <gu*****@omer.at> wrote in message
news:50**************************@posting.google.c om...
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::output(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}
/**/

----

Tanks!
-GŁnter


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
Jul 22 '05 #4

P: n/a

"Thomas Wintschel" <th******@telus.net> skrev i en meddelelse
news:c4Fvb.118975$jy.61847@clgrps13...

"G?nter Omer" <gu*****@omer.at> wrote in message
news:50**************************@posting.google.c om...
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::output(void)
{
myIterator = myList.begin();
xy *myXY = *myIterator;
cout << myXY->getI() << endl;
myList.pop_back();

}
/**/

----

Tanks!
-GŁnter


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
Jul 22 '05 #5

P: n/a
Thomas Wintschel wrote:
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.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.