473,322 Members | 1,703 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,322 software developers and data experts.

basic question on c++ linking

It is a very basic question.Surely i got something wrong in my basic
understanding.

//Contents of file1.cpp

using namespace std;
#include <iostream>

template <typename T>
class my_stack;

int main(){
my_stack<intst1;

int top_element;
top_element = st1.pop();
cout << "Hello World !! Top = " << top_element << endl;
return 0;
}

The class my_stack is defined in another file. say (template.cpp)

file.o template.o -project

why does it give linker error, since i provided the class definition
while linking too !!!

../src/file1.cpp: In function ‘int main()’:
../src/file1.cpp:8: error: ‘my_stack’ was not declared in this scope
Jun 29 '08 #1
9 1577
Peskov Dmitry wrote:
It is a very basic question.Surely i got something wrong in my basic
understanding.

//Contents of file1.cpp

using namespace std;
#include <iostream>

template <typename T>
class my_stack;

int main(){
my_stack<intst1;

int top_element;
top_element = st1.pop();
cout << "Hello World !! Top = " << top_element << endl;
return 0;
}

The class my_stack is defined in another file.
It's not a class. It's a class template.
say (template.cpp)

file.o template.o -project

why does it give linker error, since i provided the class definition
while linking too !!!
A class template is not a class. It's a description for the compiler that
tells it how to build the class once it knows the template arguments. But
for that, the compiler must know the template's definition, not just a
declaration.

Jun 29 '08 #2
Ok ...

It is not working even for a class definition.
Jun 29 '08 #3
Peskov Dmitry wrote:
Ok ...

It is not working even for a class definition.
What does not work, and how does it not work? Show some code and the error
you get.

Jun 29 '08 #4
On 2008-06-29 10:47, Peskov Dmitry wrote:
Ok ...

It is not working even for a class definition.
Please quote the text you are replying to.

You do not have a link-error but a compile-error because you are trying
to use an undeclared and undefined type (my_stack). To solve the problem
you need to include the header-file of my_stack (which should contain
the class definition) in file1.cpp.

Notice also that if you put the template definition in the header-file
and include it (i.e. rename template.cpp to template.h) you can use the
template class in file1.cpp.

--
Erik Wikström
Jun 29 '08 #5
On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.dewrote:
Peskov Dmitry wrote:
It is a very basic question.Surely i got something wrong in my basic
understanding.
//Contents of file1.cpp
using namespace std;
#include <iostream>
template <typename T>
class my_stack;
int main(){
my_stack<intst1;
int top_element;
top_element = st1.pop();
cout << "Hello World !! Top = " << top_element << endl;
return 0;
}
The class my_stack is defined in another file.
It's not a class. It's a class template.
say (template.cpp)
file.o template.o -project
why does it give linker error, since i provided the class
definition while linking too !!!
A class template is not a class. It's a description for the
compiler that tells it how to build the class once it knows
the template arguments. But for that, the compiler must know
the template's definition, not just a declaration.
But that doesn't really change anything here. The statement
template< typename T class my_stack ;
is a declaration, not a definition. The same would be true
without the "template< typename T >" (although it would declare
a class, rather than a class template).

Certain uses require a definition; a simple declaration is not
sufficient. Instantiating a template is one, and defining a
variable with a given type is another. Since he does both in
main, a definition must be available, and he's not made one
available.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 29 '08 #6
On Jun 29, 3:04*pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.dewrote:
Peskov Dmitry wrote:
It is a very basic question.Surely i got something wrong in my basic
understanding.
//Contents of file1.cpp
using namespace std;
#include <iostream>
template <typename T>
class my_stack;
int main(){
my_stack<intst1;
int top_element;
top_element = st1.pop();
cout << "Hello World *!! Top = *" << top_element << endl;
return 0;
}
The class my_stack is defined in another file.
It's not a class. It's a class template.
say (template.cpp)
file.o template.o -project
why does it give linker error, since i provided the class
definition while linking too !!!
A class template is not a class. It's a description for the
compiler that tells it how to build the class once it knows
the template arguments. But for that, the compiler must know
the template's definition, not just a declaration.

But that doesn't really change anything here. *The statement
* * template< typename T class my_stack ;
is a declaration, not a definition. *The same would be true
without the "template< typename T >" (although it would declare
a class, rather than a class template).

Certain uses require a definition; a simple declaration is not
sufficient. *Instantiating a template is one, and defining a
variable with a given type is another. *Since he does both in
main, a definition must be available, and he's not made one
available.

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Let me put my question differently ? which one below

a)
class new_class;

or

b)
class new_class
{
int a;
public:
new_class();
};

is a class declaration ?

Because if i include (b) in my code as this one it works.

using namespace std;
#include <iostream>

class new_class
{
int a;
public:
new_class();

};

int main(){
new_class st1;
return 0;
}
I assume class definition includes the function that you are defining
something like
class new_class
{
int a;
public:
new_class();
};

new_class::new_class
{
a = 10;
}

is a class definition ?

Please let me know if my understanding is incorrect ?
Jun 29 '08 #7
On 2008-06-29 13:10, Peskov Dmitry wrote:
On Jun 29, 3:04 pm, James Kanze <james.ka...@gmail.comwrote:
>On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.dewrote:
Peskov Dmitry wrote:
It is a very basic question.Surely i got something wrong in my basic
understanding.
//Contents of file1.cpp
using namespace std;
#include <iostream>
template <typename T>
class my_stack;
int main(){
my_stack<intst1;
int top_element;
top_element = st1.pop();
cout << "Hello World !! Top = " << top_element << endl;
return 0;
}
The class my_stack is defined in another file.
It's not a class. It's a class template.
say (template.cpp)
file.o template.o -project
why does it give linker error, since i provided the class
definition while linking too !!!
A class template is not a class. It's a description for the
compiler that tells it how to build the class once it knows
the template arguments. But for that, the compiler must know
the template's definition, not just a declaration.

But that doesn't really change anything here. The statement
template< typename T class my_stack ;
is a declaration, not a definition. The same would be true
without the "template< typename T >" (although it would declare
a class, rather than a class template).

Certain uses require a definition; a simple declaration is not
sufficient. Instantiating a template is one, and defining a
variable with a given type is another. Since he does both in
main, a definition must be available, and he's not made one
available.
Please do not quota signatures.
Let me put my question differently ? which one below

a)
class new_class;
This is a declaration, it tells the compiler that there exists a class
called new_class, but nothing else. If all you need is a pointer to the
class a declaration is enough, for just about anything else you need the
definition too.
b)
class new_class
{
int a;
public:
new_class();
};
This is the definition, it tells the compiler what the new_class is (how
much memory an instance needs, what members it has etc.). Notice that a
definition is also a declaration, you can declare a type as many times
as you want but you can only define it once.
Because if i include (b) in my code as this one it works.

using namespace std;
#include <iostream>

class new_class
{
int a;
public:
new_class();

};

int main(){
new_class st1;
return 0;
}
You can also put the definition of the class in a header-file and incude
that in the .cpp-files that needs it.
I assume class definition includes the function that you are defining
something like
class new_class
{
int a;
public:
new_class();
};

new_class::new_class
{
a = 10;
}
No, that is the definition of the member function (the constructor in
this case), and it does not have to be included with the class
definition. Actually it is quite common to separate the class definition
from the definitions of its member-functions like this:

some_class.h:
-------------

class some_class
{
int a;
double b;
public:
some_class();
int getA();
double getB()
};

some_class.cpp:
---------------

#include "some_class.h"

some_class::some_class()
{
a = 1;
b = 2.5;
}

// Actually we should use an initialisation-list instead of setting the
// values of the class-members in the constructor, but that is for
// another day.

int some_class::getA()
{
return a;
}

double some_class::getB()
{
return b;
}

Any file that needs to use some_class can then include the some_class.h
file like this:

main.cpp:
---------

#include <iostream>

#include "some_class.h"

int main()
{
some_class c;
std::cout << c.getA() << "\n" << c.getB() << std::endl;
return 0;
}

--
Erik Wikström
Jun 29 '08 #8
On Jun 29, 1:10 pm, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
On Jun 29, 3:04 pm, James Kanze <james.ka...@gmail.comwrote:
[...]
Let me put my question differently ? which one below

a)
class new_class;
or
b)
class new_class
{
int a;
public:
new_class();
};
is a class declaration ?
Both. The second is also a definition. (A definition is a
declaration, but the reverse is not necessarily true.)
Because if i include (b) in my code as this one it works.
That's because the way you used the class required a definition.
using namespace std;
#include <iostream>
class new_class
{
int a;
public:
new_class();
};
int main(){
new_class st1;
return 0;
}
I assume class definition includes the function that you are defining
something like
class new_class
{
int a;
public:
new_class();
};
A class definition includes the contents (members) of the class.
new_class::new_class
{
a = 10;
}
is a class definition ?
No, that's a syntax error.

A class definition is:
class <name<baseclass-declarations>[opt] { ... } ;
A class declaration (which isn't also a definition) is just:
class <name;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 29 '08 #9
On Jun 29, 2:03 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-06-29 13:10, Peskov Dmitry wrote:
Let me put my question differently ? which one below
a)
class new_class;
This is a declaration, it tells the compiler that there exists
a class called new_class, but nothing else. If all you need is
a pointer to the class a declaration is enough, for just about
anything else you need the definition too.
And references, of course.

Also, you can *declare* (but not define) objects of the type.
Thus, "extern new_class some_object ;" is legal with just a
class declaration. Or, within a class:

class Another
{
new_class illegal; // a non-static member is a definition
static new_class legal ;
// but a static member is just a
// declaration (which means that it
// must be defined elsewhere).
} ;
b)
class new_class
{
int a;
public:
new_class();
};
This is the definition, it tells the compiler what the
new_class is (how much memory an instance needs, what members
it has etc.). Notice that a definition is also a declaration,
you can declare a type as many times as you want but you can
only define it once.
In general. Classes (and templates and inline functions) are an
exception. You can only define them once in each translation
unit, but you can define them in as many translation units as
you want, as long as the definitions are identical.

[...]
I assume class definition includes the function that you are
defining something like
class new_class
{
int a;
public:
new_class();
};
new_class::new_class
{
a = 10;
}
No, that is the definition of the member function (the
constructor in this case),
I don't think so. To be the definition of the constructor, he'd
need to have parentheses after the second new_class.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 29 '08 #10

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

Similar topics

8
by: JotM | last post by:
Hi all, I am currently fooling around trying to produce a very basic webpage that can be shown on both computer screens and Nokia series 60 GSM cell phones featuring an XHTML browser. (XHTML-MP...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
0
by: gasturbtec | last post by:
please help im new at access programming and i just got this project dropped in my lap because the old programmer quit. i've been doing ok so far but now i need to add code to an existing database...
1
by: WillieW | last post by:
Hi folks, I have Access 97 and have set up four tables, each with a Primary Key with a file name manually entered. For example, the four tables relate to information stored in a paper file, on...
6
by: Rudy Ray Moore | last post by:
I work with a multi-project workspace. One project (the "startup" project) has a "Configuration Type" of "Application (.exe)". The other 40 projects have a "Configuration Type" of "Static Library...
0
by: Rudy Ray Moore | last post by:
I've been having trouble getting incremental linking to work under Visual C++ .net 2003 7.1 for my multi-project workspace. Ronald Laeremans and Carl Daniel (and a few others) helped me figure it...
2
by: Robert Liles | last post by:
I have recently migrated from Microsoft Professional Development System Version 7.1 (I know I am in the stone age >G<) to Microsoft Visual Basic.NET. I am used to writing time critical or very...
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
3
by: robertoathome | last post by:
Hello, I successully adapted a search form from a microsoft example into my own db. MS Example I type search parameters in 2 boxes and the results are returned in a new, basic query window. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.