Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 2nd, 2006, 05:18 PM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to XeeTu
Default error with pointer code

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 With Quote
  #2 (permalink)  
Old December 4th, 2006, 08:13 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

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 With Quote
  #3 (permalink)  
Old December 7th, 2006, 11:41 PM
Wrox Author
 
Join Date: Dec 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to struct goscho C++ Programming 1 January 10th, 2008 07:23 AM
Constant pointer and pointer to a constant [email protected] C++ Programming 2 June 5th, 2007 01:39 AM
Pointer fReqZz C++ Programming 5 June 5th, 2007 01:09 AM
Document Class pointer error bluebeta Visual C++ 3 July 5th, 2006 05:10 AM
Pointer reference perstam C++ Programming 5 April 29th, 2006 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.