|
Subject:
|
error with pointer code
|
|
Posted By:
|
XeeTu
|
Post Date:
|
12/2/2006 4:18:56 PM
|
hello, this is a code that was in the tutorial that i was following. This code explains how pointers work. I am using visual c++ .net 2003 to code this. This is the only code that i am having trouble with so far, every other code in the tutorial works fine. I checked and rechecked everything multiple times but cannot figure out what the error may be.
This is how it is listed in the tutorial and it is working perfectly for them and the tutorial also uses the same software which is visual c++ .net 2003.
#include <iostream>
using namespace std;
void main() { int *ptr;
ptr = new int[5];
*ptr = 5; ptr[1] = 10; ptr += 2; *ptr = 15; ptr -= 2;
for (int i = 0; i < 5; i++) { cout << ptr[i] << endl; }
delete ptr; }
I am getting error of error LNK2005: _main already defined in pointers.obj
and error
fatal error LNK1169: one or more multiply defined symbols found
|
|
Reply By:
|
Geo121
|
Reply Date:
|
12/4/2006 7:13:42 AM
|
I'm not exactly familiar with the differences in C++ and visual c++ .net 2003 but the error you are getting sounds and looks like a linker error.
Now because I'm not sure what visual c++ .net 2003 does I'm not sure if you need to declare you main function as an int.
so if you use this code :
#include <iostream>
using namespace std;
int main() { int *ptr;
ptr = new int[5];
*ptr = 5; ptr[1] = 10; ptr += 2; *ptr = 15; ptr -= 2;
for (int i = 0; i < 5; i++) { cout << ptr[i] << endl; }
delete ptr; return 0; }
It should work, It does for me, but I'm not using visual c++
Sorry I can't help anymore than this
~ Geo
~ You are unique, just like everyone else
|
|
Reply By:
|
steven_wort
|
Reply Date:
|
12/7/2006 10:41:31 PM
|
It looks like you have multiple OBJ files with a _main defined.
For simple / trivial code samples like this, I find it MUCH easier to compile on the command line. It tecaches me the correct compiler and linker options, and is so much easier to troubleshoot when something goes wrong.
To get started use the Visual Studio Command Prompt (found under Start -> Microsoft Visual Studio <version> -> Visual Studio <version> Tool -> Visual Studio command prompt
Once you have this open navigate to the folder where your source file lives and then use the following command line to compile the sample below. I called mine sample.cpp
cl sample.cpp /c /EHsc
After the compiler is done, then link it using the following example.
cl sample.obj /link
If you want to debug it, then you need a few more options, the following is a comprehensive set of compile and link settings to build a symbol files, assembly listings, and a map file to help with debugging.
cl sample.cpp /c /Zi /MTd /Od /Fdsample.pdb /FAcs /EHsc
cl sample.obj /DEBUG /link /PDB:sample.pdb /OUT:sample.exe /release /DEBUGTYPE:cv /map If you want to know what all the options stand for, go look them up in the visual studio help files. I usualy search MSDN for compiler options, and that gets me a good hit somewhere in the online version of msdn.
http://msdn2.microsoft.com/en-US/library/9s7c9wdw.aspx
The linker options are the next section below this.
Hope this helps get you going again.
Steve Wort Co Author "Professional SQL Server 2005 Administration"
|