473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking problem - please help

I'm in the early stages of developing a class that will represent a metric
distance by storing both a number and unit (ie KM, M, CM etc).

I've developed some initial code as a starting point; however, it won't link
during a compile using VC++.

The error message I get is "
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup

Debug/Message.exe : fatal error LNK1120: 1 unresolved externals"
I've included the header and source below. Any help would be appreciated.

Thanks
//Distance.h as follows:
********************************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance ( int, char ) ; // constructor (takes int value and string unit of
measure

~Distance ( void ) ; // destructor (its name is ~ then class name)

//access member functions

int number (void) const;

char measure (void) const;

private :

int nu ; // the value

char me ; // the unit of measure

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif

//Distance.cpp as follows:

************************************************** ****
#include "Distance.h"

#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructor

Distance :: Distance ( int n, char m ) : nu(n), me(m) {}

// access functions

int Distance :: number (void) const

{

return nu;

}

char Distance :: measure (void) const

{

return me;

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << d.number() << "-" << d.measure();

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )

{

// create test input

Distance a = Distance (5, KM);

cout << a << endl;

cin ignore();

return 0; // normal termination

}

#endif


Jul 22 '05 #1
12 1573
Chiller wrote:

I'm in the early stages of developing a class that will represent a metric
distance by storing both a number and unit (ie KM, M, CM etc).

I've developed some initial code as a starting point; however, it won't link
during a compile using VC++.

The error message I get is "
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup

as the linker says: it cannot find a main function in what you try to link.

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )
[...]
#endif


I cannot see a definition for TEST_DISTANCE in what you have posted. Is this
definement set through some compiler options?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Chiller wrote:

I'm in the early stages of developing a class that will represent a metric
distance by storing both a number and unit (ie KM, M, CM etc).

I've developed some initial code as a starting point; however, it won't link
during a compile using VC++.

The error message I get is "
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup

as the linker says: it cannot find a main function in what you try to link.

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )
[...]
#endif


I cannot see a definition for TEST_DISTANCE in what you have posted. Is this
definement set through some compiler options?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
>
#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )

{

// create test input

Distance a = Distance (5, KM);

cout << a << endl;

cin ignore();

return 0; // normal termination

}

#endif


The obvious problem seems to be that you haven't defined TEST_DISTANCE. Are
you sure you developed this code? And what do you think TEST_DISTANCE is for
anyway? I would just remove it.

john
Jul 22 '05 #4
>
#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )

{

// create test input

Distance a = Distance (5, KM);

cout << a << endl;

cin ignore();

return 0; // normal termination

}

#endif


The obvious problem seems to be that you haven't defined TEST_DISTANCE. Are
you sure you developed this code? And what do you think TEST_DISTANCE is for
anyway? I would just remove it.

john
Jul 22 '05 #5
I was under the impression that a test driver could be included with a class
which would enable conditional compilation by wrapping the test in "#ifdef
TEST_?????" and #endif. The ????? being the name of the class in uppercase.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Chiller wrote:

I'm in the early stages of developing a class that will represent a metric distance by storing both a number and unit (ie KM, M, CM etc).

I've developed some initial code as a starting point; however, it won't link during a compile using VC++.

The error message I get is "
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup

as the linker says: it cannot find a main function in what you try to

link.

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )
[...]

#endif


I cannot see a definition for TEST_DISTANCE in what you have posted. Is

this definement set through some compiler options?

--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #6
I was under the impression that a test driver could be included with a class
which would enable conditional compilation by wrapping the test in "#ifdef
TEST_?????" and #endif. The ????? being the name of the class in uppercase.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Chiller wrote:

I'm in the early stages of developing a class that will represent a metric distance by storing both a number and unit (ie KM, M, CM etc).

I've developed some initial code as a starting point; however, it won't link during a compile using VC++.

The error message I get is "
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup

as the linker says: it cannot find a main function in what you try to

link.

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main ( void )
[...]

#endif


I cannot see a definition for TEST_DISTANCE in what you have posted. Is

this definement set through some compiler options?

--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #7

"Chiller" <...@...> wrote in message
news:2a******************************@news.teranew s.com...
I was under the impression that a test driver could be included with a class which would enable conditional compilation by wrapping the test in "#ifdef
TEST_?????" and #endif. The ????? being the name of the class in uppercase.


Well, that's not right.

Seems you are under the impression that the C++ pre-processor is more
sophisticated than it really is. The rules are very simple, if you write

#ifdef SOMETHING

some code in here

#endif

then the code between the #ifdef and #endif will not be compiled unless
SOMETHING is defined.

That's all there is to it, test drivers and classes have no relevance.

john
Jul 22 '05 #8

"Chiller" <...@...> wrote in message
news:2a******************************@news.teranew s.com...
I was under the impression that a test driver could be included with a class which would enable conditional compilation by wrapping the test in "#ifdef
TEST_?????" and #endif. The ????? being the name of the class in uppercase.


Well, that's not right.

Seems you are under the impression that the C++ pre-processor is more
sophisticated than it really is. The rules are very simple, if you write

#ifdef SOMETHING

some code in here

#endif

then the code between the #ifdef and #endif will not be compiled unless
SOMETHING is defined.

That's all there is to it, test drivers and classes have no relevance.

john
Jul 22 '05 #9
For anyone reading this thread.

As it turns out, I'm also required to declare the test driver in the
preprocessor definitions. This must avoid the requirement to declare or
undeclare the definition each time I wish to use the test driver.

To do this under VC++ it's simply a matter of going into the properties of
the project selecting the Preprocessor and adding the name of the test
driver, "TEST_DISTANCE" in this case.
Thanks for your help Karl.

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"Chiller" <...@...> wrote in message
news:2a******************************@news.teranew s.com...
I was under the impression that a test driver could be included with a

class
which would enable conditional compilation by wrapping the test in "#ifdef TEST_?????" and #endif. The ????? being the name of the class in

uppercase.


Well, that's not right.

Seems you are under the impression that the C++ pre-processor is more
sophisticated than it really is. The rules are very simple, if you write

#ifdef SOMETHING

some code in here

#endif

then the code between the #ifdef and #endif will not be compiled unless
SOMETHING is defined.

That's all there is to it, test drivers and classes have no relevance.

john

Jul 22 '05 #10
For anyone reading this thread.

As it turns out, I'm also required to declare the test driver in the
preprocessor definitions. This must avoid the requirement to declare or
undeclare the definition each time I wish to use the test driver.

To do this under VC++ it's simply a matter of going into the properties of
the project selecting the Preprocessor and adding the name of the test
driver, "TEST_DISTANCE" in this case.
Thanks for your help Karl.

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...

"Chiller" <...@...> wrote in message
news:2a******************************@news.teranew s.com...
I was under the impression that a test driver could be included with a

class
which would enable conditional compilation by wrapping the test in "#ifdef TEST_?????" and #endif. The ????? being the name of the class in

uppercase.


Well, that's not right.

Seems you are under the impression that the C++ pre-processor is more
sophisticated than it really is. The rules are very simple, if you write

#ifdef SOMETHING

some code in here

#endif

then the code between the #ifdef and #endif will not be compiled unless
SOMETHING is defined.

That's all there is to it, test drivers and classes have no relevance.

john

Jul 22 '05 #11
Chiller wrote:

For anyone reading this thread.

As it turns out, I'm also required to declare the test driver in the
preprocessor definitions. This must avoid the requirement to declare or
undeclare the definition each time I wish to use the test driver.

To do this under VC++ it's simply a matter of going into the properties of
the project selecting the Preprocessor and adding the name of the test
driver, "TEST_DISTANCE" in this case.

Thanks for your help Karl.


2 things:

* Please don't top post. Put your reply beneth the text you are replying to,
such I have done it right now.

* Again: test driver or some other high sophiticated concept has nothing
to do with it. It's simple a preprocessor macro, that's used to include
or exclude some text. That this text implements the main() function is
a coincidence. It could be anything else

#include <iostream>

#define MY_TEST

int main()
{

#ifdef MY_TEST
std::cout << "MY_TEST is defined" << std::endl;
#else
std::cout << "MY_TEST is NOT defined" << std::endl;
#endif
}

If you compile and run this program, it will output
MY_TEST is defined

If you then comment the #define line such as

#include <iostream>

// #define MY_TEST

int main()
...

it will output
MY_TEST is NOT defined

It's a simple question of what source code text gets compiled. Parts
of the source code text can be hidden by using an #ifdef preprocessor
directive. The preprocessor knows next to nothing about C or C++. All
it does is some text replacements before the actual compiler sees the
source code. That's all it does.

Most compilers also allow to #define or #undef - ine preprocessor symbols
via the command line interface, or the IDE. But in principle it is the same
thing: if the symbol is defined some text gets sent to the compiler, if it
is not, the text is hidden from the compiler. In this case. Such preprocessor
macros and symbols can be used for a number of other things. But it always
turns around: modify the source code text before the actual compiler sees it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12
Chiller wrote:

For anyone reading this thread.

As it turns out, I'm also required to declare the test driver in the
preprocessor definitions. This must avoid the requirement to declare or
undeclare the definition each time I wish to use the test driver.

To do this under VC++ it's simply a matter of going into the properties of
the project selecting the Preprocessor and adding the name of the test
driver, "TEST_DISTANCE" in this case.

Thanks for your help Karl.


2 things:

* Please don't top post. Put your reply beneth the text you are replying to,
such I have done it right now.

* Again: test driver or some other high sophiticated concept has nothing
to do with it. It's simple a preprocessor macro, that's used to include
or exclude some text. That this text implements the main() function is
a coincidence. It could be anything else

#include <iostream>

#define MY_TEST

int main()
{

#ifdef MY_TEST
std::cout << "MY_TEST is defined" << std::endl;
#else
std::cout << "MY_TEST is NOT defined" << std::endl;
#endif
}

If you compile and run this program, it will output
MY_TEST is defined

If you then comment the #define line such as

#include <iostream>

// #define MY_TEST

int main()
...

it will output
MY_TEST is NOT defined

It's a simple question of what source code text gets compiled. Parts
of the source code text can be hidden by using an #ifdef preprocessor
directive. The preprocessor knows next to nothing about C or C++. All
it does is some text replacements before the actual compiler sees the
source code. That's all it does.

Most compilers also allow to #define or #undef - ine preprocessor symbols
via the command line interface, or the IDE. But in principle it is the same
thing: if the symbol is defined some text gets sent to the compiler, if it
is not, the text is hidden from the compiler. In this case. Such preprocessor
macros and symbols can be used for a number of other things. But it always
turns around: modify the source code text before the actual compiler sees it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #13

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

Similar topics

5
1594
by: Rudy | last post by:
I am desperately seeking some help with a program that was supposed to have been completed last night. I have only been working with PHP for a week. I have tried and tried and tried, but I am...
0
2215
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
1207
by: bab | last post by:
i need help linking my relational data base. some one help me please. i can link a one to one relationship but i cant link a one to many for some unknown reason. can some one please tell me how to...
4
3005
by: Gary Hughes | last post by:
Hi all, sometime I posted a problem in here where I was getting the following error from the linker in VS C++ 2003. Linking... GCClass.obj : error LNK2022: metadata operation failed (80131188)...
2
7226
by: | last post by:
Help! I'm new to c++, and am breaking my teeth on MS Visual C++ (bundled within Visual Studio .NET 2003). Am trying to link simple c++ code to fortran dlls created in Compaq Visual Fortran (v6.1)....
7
2362
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
1
2226
by: buchalino | last post by:
Hi Guys, Please can someone help me, I am having a linking problem . I am writing a socket program, the problem is just the linking . I am using VC++ In the process of the problem, I...
1
5942
by: srikar | last post by:
what is the difference between static linking & dynamic linking, what are the advantages of each? How to perform static linking & Dynamic linking by using gcc -o liniking will be done , but...
0
3959
by: xieml2007 | last post by:
Dear Madam or Sir, I encountered one problem which is quite similiar to the discussions launched at the web site: http://www.thescripts.com/forum/thread280324.html
0
1420
NeoPa
by: NeoPa | last post by:
If anyone can help with a problem linking across to a table in a database on a SQL 2000 server, then please visit Linking from Access over in SQL Server. I'm locking this so that any and all...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
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...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
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...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
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...

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.