make "new" object

This is a discussion on make "new" object within the C++ Programming forums, part of the General Programming Boards category; Hello. Im trying to make a new object into my main(): Code: objectName name = new objectName(); Where "objectName" is ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    make "new" object

    Hello. Im trying to make a new object into my main():

    Code:
    objectName name = new objectName();
    Where "objectName" is a class into my Header file.
    But Im reciving the error:

    error C2440: 'initializing' : cannot convert from 'objectName *' to 'objectName'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Any idea what I have missed? Im not very familiar with pointers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Coming from say, Java, are you?

    Just use:
    Code:
    objectName name;
    With new you get a pointer and use dynamic memory allocation, but it appears that you have not learnt about pointers yet and probably do not need dynamic memory allocation here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    Thanks. I will try to make it without "new" then. But this raise other errors.
    I want to put my "className name;" into a vector created/declare into my Header file like this:

    Code:
    static vector<className*> vname;
    and into my main():

    Code:
    className::vname.push_back(name);
    But this gives me the error:
    error: cannot convert from 'className' to 'className *const '
    Seems like a pointer problem again =(. Any ideas?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can either store the actual object, or an address to the object in the vector. There are good reasons for doing either, it depends very much on what you actually plan on doing with the content of the vector (and how you create your data).

    In the simple case presented here, I'd say "use className" instead of "className *" for the template of your vector. The alternative is to add a * to your name in main and change the line back to have a "new" in it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    Thanks a lot matsp. I removed the pointer and "new". But now I would like to run a method who is located into the source file of the Header file.

    Code:
    className::vname[0].methodName();
    But getting the strange error:

    main.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class className,class std::allocator<class className> > className::vname" (?vname@className@@2V?$vector@VclassName@@V?$alloc ator@VclassName@@@std@@@std@@A)
    .\Debug/OpenGL_Lab.exe : fatal error LNK1120: 1 unresolved externals
    any clue why Im getting this? Dont understand the error at all.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, but that I can't answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Maybe it's not a static function.... lol...

    /me shrugs.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hmm. What does your header file look like. Is the vector declared inside className?

    If at all possible, post the headerfile (or if it's really large, the portions where your className and vector parts are declared.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it is a static member of the class, it needs to be defined (or is it initialized) somewhere. So, an implementation file would need this line:

    Code:
    vector<className> ClassName::vname;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    If it is a static member of the class, it needs to be defined (or is it initialized) somewhere. So, an implementation file would need this line:

    Code:
    vector<className> ClassName::vname;
    Pretty obvious, really! Didn't see the static part...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 09:23 AM
  2. Reviving my old scripting language.
    By suzakugaiden in forum Game Programming
    Replies: 4
    Last Post: 06-15-2006, 04:02 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  5. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21