How to use microsoft Visual C++ 2005 Express edition beta (linking source files)

Started by
7 comments, last by Scipio3 18 years ago
I've been reading a book on C++ and have started trying to do some of the exercises. I've started off writing a simple program that has one function called "addition" that adds two integers. I can get it to work when I put main(), the function prototype, and the function definition in one file. But if I put the definition in one file, the prototype in a header, and main in another .cpp source file, it doesn't work. Is there anything special that must be done here? I made a project in C++ and added the function definition's source file to the project. Is the problem that you need to do something to make the files link together in the visual C++ interface, or do I need do something to the code to get the two source files to link. I'm also able to get it to work if the definition and main are in the same .cpp file and the prototype is in an included header. It seems like I can understand the book, but I'm having trouble using the development interface. -- Scipio3
Advertisement
Perhaps if we could see some code? I just ran a test with the function prototype in one header, the definition in one source file, and the main() in another source file and it ran just fine. Here's what I did:

add.h:

int addition(int num1, int num2);



add.cpp:
int addition(int num1, int num2){    return num1 + num2;}



main.cpp:
#include <stdlib.h>#include <math.h>#include <stdio.h>#include <conio.h>#include "add.h"int main(){    printf("%d", addition(1, 1));    getch();    return 0;}


Console output: 2

That all compiled and ran fine, so maybe we could see your code and go from there. Hope it helps.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
There are 3 files-- definition1.cpp, header1.h, firstprogram.cpp

I was able to run them when they were in the same file.

Is there anything wrong with the code, or am I using the interface (visual C++) incorrectly. All I've done is made a project, added one header and one .cpp file and just select build from the build menu (build "project").

It might be the case that i introduced some error when I copy/pasted them into the different files.


definition1.cpp:


int Add(int a, int b)
{
int c;
c=a+b;

return c;
}


header1.h:

int Add(int, int);


firstprogram.cpp:

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



int _tmain(int argc, _TCHAR* argv[])

{using std::cout;

cout << "60+70 = :" << Add(60,70);




return 0;
}



-- scipio3
I couldn't see anything immediately wrong with that. Perhaps you could explain what you mean by "it didn't work"? Was there a compile time error? What was it?

EDIT: Also, it's easier on the eyes to read source with tags. That is, for each snippit of code place it between [ source] CODE HERE [ /source] except with no space between the brackets.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:
All I've done is made a project, added one header and one .cpp file and just select build from the build menu (build "project")


The project needs to include all the .cpp files. Not just one.
Here's the big ask: What are the errors Visual C++ is throwing up?
If you want a quick and comprehensive reply -- be sure to let us know just precisely what is wrong ;)

~Shiny.
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
A couple of things that come to mind:


1. Put the <iostream> header into the stdafx.h file. You are never going to change that header file and therefore its a great candidate for a precompiled header. Typically, leave the ones you author out. This will increase your compile times significantly.

2. Being as you are using precompiled headers I am going to guess that you are trying to comile either of the cpp files and getting the error - possibly saying it cannot find the procompiled stuff..


If #2 is the case you can do one of two things, either build the SOLUTION (CTRL+SHIFT+B I think...) or R-Click on the Stdafx.cpp, build it first - and then build the other cpp files.

If thats not the case, then as said before - more data.


Hope that helps..

#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
This is what it says when I try to build.



------ Build started: Project: First program, Configuration: Debug Win32 ------
Compiling...
definitiion1.cpp
c:\documents and settings\roy sr\my documents\visual studio 2005\projects\first program\first program\definitiion1.cpp(10) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
Build log was saved at "file://c:\Documents and Settings\Roy Sr\My Documents\Visual Studio 2005\Projects\First program\First program\Debug\BuildLog.htm"
First program - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

--Scipio3
I got it working.

When I posted the info the program was giving me I was first able to read it.

After I included the stdafx.h file in the other source file (definition.cpp).

--Scipio3

This topic is closed to new replies.

Advertisement