PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > basic question on c++ linking
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
basic question on c++ linking

Réponse
 
LinkBack Outils de la discussion
Vieux 29/06/2008, 09h19   #1
Peskov Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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<int> st1;

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
  Réponse avec citation
Vieux 29/06/2008, 09h26   #2
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

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<int> st1;
>
> 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.

  Réponse avec citation
Vieux 29/06/2008, 09h47   #3
Peskov Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

Ok ...

It is not working even for a class definition.
  Réponse avec citation
Vieux 29/06/2008, 10h03   #4
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

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.

  Réponse avec citation
Vieux 29/06/2008, 10h25   #5
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

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
  Réponse avec citation
Vieux 29/06/2008, 11h04   #6
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.de> wrote:
> 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<int> st1;


> > 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.kanze@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
  Réponse avec citation
Vieux 29/06/2008, 12h10   #7
Peskov Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

On Jun 29, 3:04pm, James Kanze <james.ka...@gmail.com> wrote:
> On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.de> wrote:
>
>
>
> > 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<int> st1;
> > > 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 ?
  Réponse avec citation
Vieux 29/06/2008, 13h03   #8
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: basic question on c++ linking

On 2008-06-29 13:10, Peskov Dmitry wrote:
> On Jun 29, 3:04 pm, James Kanze <james.ka...@gmail.com> wrote:
>> On Jun 29, 10:26 am, Rolf Magnus <ramag...@t-online.de> wrote:
>>
>>
>>
>> > 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<int> st1;
>> > > 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19337 seconds with 16 queries