473,796 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1608
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 objektorientier ter Datenverarbeitu ng
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...@gm ail.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 objektorientier ter Datenverarbeitu ng
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...@gm ail.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::som e_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::get A()
{
return a;
}

double some_class::get B()
{
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...@g mail.comwrote:
On Jun 29, 3:04 pm, James Kanze <james.ka...@gm ail.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<basecla ss-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 objektorientier ter Datenverarbeitu ng
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 objektorientier ter Datenverarbeitu ng
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
1652
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 | XHTML Mobile Profile) I found XHTML-MP to be the W3C XHTML-Basic + WML extensions, that is why I have chosen to base a web page on XHTML Basic 1.0 Doing that I am running into the following problem: Nokia phones do not support the media...
20
3242
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 references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
0
2249
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 that is used to connect to other databases and generate reports. below is sample code of how the database does the linking i hope i give you enough info to help me but if not let me know and i will give more. Sub txtShipDataFileSub() Dim...
1
11725
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 four subjects, the paper file is indexed WM/01/05. This number, a unique identifier in the database, is the primary key and the same content in each table. Table 1 is called "Driver Data" Table 2 is called "Vehicle Owner"
6
6579
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 (.lib)". My question: Should the linker incrementally link when I make a change to one of the ..cpps in one of my .lib projects? For VC6 the answer is yes.
0
1674
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 out. Short answer: I should never have expected incremental linking to work. Short answer addendum: Linking is slower in 7.1/.net/2003 than VC++6. ===
2
1002
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 repetitive routines in Assembly language and linking them into my Basic PDS programs to greatly enhance execution speed. I can find no information on doing this in VB.NET. Can someone point me to a resource that will tell me how to pass arguments...
2
10007
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 comes like Error 1 error C2371: 'WCHAR' : redefinition; different basic types c: \program files\microsoft visual studio 8\vc\platformsdk\include \wabdefs.h 78
3
1827
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. I created: 2 text boxes 1 Command button ( search ) 1 Macro to open the query
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.