Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multilple/Function
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Junaid2pac




PostPosted: Thu Apr 12, 2007 3:58 pm   Post subject: Multilple/Function

I have just basically started learning c++ and have come to a chapter on multiple files and when i run the program it gives me the following error:

SVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Junaid C\C++\Multiple Files 2\Real Test\Debug\Real Test.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Junaid C\C++\Multiple Files 2\Real Test\Debug\BuildLog.htm"
Real Test - 2 error(s), 0 warning(s)

i put this in header:

double Square (double x);
double Cube (double x);
---------------------------------
The function file:
---------------------------------
#include "stdafx.h"


double Square (double value)
{
double squareReturn;
squareReturn = value * value;
return squareReturn;
}

double Cube(double value)
{
double cubeReturn;
cubeReturn = value * value * value;
return cubeReturn;
}

--------------------------
And then a little Test:
--------------------------

#include "stdafx.h"
#include <iostream>

using namespace std;

int Main (void)
{
double num = 5;
double ans;

ans = Square(num);
cout << "Ans: " << ans;

system("pause");
return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
Junaid2pac




PostPosted: Thu Apr 12, 2007 4:01 pm   Post subject: Re: Multilple/Function

Help
md




PostPosted: Thu Apr 12, 2007 4:39 pm   Post subject: RE:Multilple/Function

3 minutes is not long enough to expect a response.

First issue: pre-compiled headers. You don't need them, and have no idea what they are. Disable them and remove the related files/code. MSVC _sucks_ at things like that.

Second: code does not go in headers. I cannot stress that enough. If you want to use multiple files then you need to have multiple cpp files. In the .h/.hpp files you would then put a function header so that the compiler knows how to call functions. Since your using MSVC it'll handle all the linking for you.
Junaid2pac




PostPosted: Thu Apr 12, 2007 5:13 pm   Post subject: Re: Multilple/Function

Can u please elaborate on ur second point.....my head hurts:

Second: code does not go in headers. I cannot stress that enough. If you want to use multiple files then you need to have multiple cpp files. In the .h/.hpp files you would then put a function header so that the compiler knows how to call functions. Since your using MSVC it'll handle all the linking for you.
md




PostPosted: Thu Apr 12, 2007 7:02 pm   Post subject: RE:Multilple/Function

Header files contain function headers, type declarations and things of that nature. Code files contain actual code.

.H/.hpp are header files; .c/c.pp are code files.

Since you said you were learning from a book and you say which book? So far it seems that it's pretty bad for not teaching these things before multiple files.
OneOffDriveByPoster




PostPosted: Thu Apr 12, 2007 11:52 pm   Post subject: Re: Multilple/Function

Junaid2pac @ Thu Apr 12, 2007 3:58 pm wrote:
code:
int Main (void)

C++ is case sensitive; use lowercase for "main".
Use "code" tags--I think md got confused over what files you have because of the formatting.
Also, I don't think you need stdafx.h, although I haven't really used MSVC that much...
md




PostPosted: Fri Apr 13, 2007 6:39 am   Post subject: RE:Multilple/Function

Yup, I completely missed that; it'd be teh cause of teh error.
klopyrev




PostPosted: Fri Apr 13, 2007 6:49 am   Post subject: Re: Multilple/Function

Code doesn't go into header files? Ever look at stl_algo.h? 178kb of code (at least with my compiler)
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Fri Apr 13, 2007 8:51 am   Post subject: RE:Multilple/Function

Templates and class defs are not code; even when they contain inline functions (because they're inline).

Code is function bodies, and static/global variables, or other things of that nature. You should also keep anything that isn't needed outside of a particular class/library hidden from everything else except that class/library. All too often I see people write code that is entirely dependent on side effects hidden within functions that are named in ways having nothing to do with what they are actually doing. It makes debugging code next to impossible.
wtd




PostPosted: Fri Apr 13, 2007 3:44 pm   Post subject: RE:Multilple/Function

Templated code is not executable code. It is a template that the compiler uses to generate executable code.
Junaid2pac




PostPosted: Sat Apr 14, 2007 10:48 pm   Post subject: Re: Multilple/Function

K, thanx i figured it out and got it working!!!
Junaid2pac




PostPosted: Sat Apr 14, 2007 10:59 pm   Post subject: Re: Multilple/Function

I just forgot to add this to the header:

#pragma once
wtd




PostPosted: Sun Apr 15, 2007 11:11 am   Post subject: RE:Multilple/Function

You would be better off understanding how to properly write multimple inclusion guards.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: