Ask Experts Questions for FREE Help !
Ask
    Ribbonic Plague's Avatar
    Ribbonic Plague Posts: 19, Reputation: 2
    New Member
     
    #1

    May 12, 2009, 08:17 AM
    Simple C++ error: Cannot convert at initialization
    Hey guys. I have a very simple code that's spewing out an error:

    Code:
    #include <iostream>
    #include <string>
    #include "NodeWord.h"
    using namespace std;
    
    NodeWord::~NodeWord(){
    
    }
    
    NodeWord::NodeWord(string& word){
            data = word;
    }
    
    
    int main()
    {
            NodeWord * n("New Word");
            cout << n->data << endl;
            return 0;
    }
    Where my NodeWord.h is simple a header file of the above constructor/destructor, plus:
    Code:
    string data;
    All are public.

    Now when I try to compile it, it gives me the following error:
    Code:
    error: cannot convert 'const char*' to 'NodeWord*' in initialization
    Naturally, without making n a pointer it works, but as a pointer it doesn't work. This might be a very simple error on my part (complete noob at C++) but any help will be very, VERY APPRECIATED!

    Cheers!
    Libran's Avatar
    Libran Posts: 43, Reputation: 0
    Junior Member
     
    #2

    May 20, 2009, 06:56 AM

    You can't copy one string into another just my using = sign.

    I hope this helps and will not produce errors in other places of your program.
    Ribbonic Plague's Avatar
    Ribbonic Plague Posts: 19, Reputation: 2
    New Member
     
    #3

    May 20, 2009, 04:44 PM

    Er actually you can:
    operator= - C++ Reference

    The operator = is already overloaded in the string class, so you can. In C that's illegal, but in C++ it's OK.

    It's OK, I've understood the problem. I guess it kind of makes sense that you can't create a new constructor with a pointer because, well, a pointer is a pointer TO an object, not an object itself. So to make my code work instead of this:

    Code:
    NodeWord * n("New Word");
    return n;
    I use something like:

    Code:
    NodeWord n("New Word");
    NodeWord * node = &n;
    return node;
    It was indeed a noob problem which I should have seen the moment I received the error *facepalm* but I hope others will learn from this!
    gurinderc's Avatar
    gurinderc Posts: 7, Reputation: 2
    New Member
     
    #4

    Oct 4, 2009, 10:16 AM

    In short -

    NodeWord * n = new NodeWord ("New Word");

    does it makes sense where you are wrong?

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

DLL initialization failed [ 2 Answers ]

Hai, I developed an appl. Using vb6.0 as front end and visual foxpro as backend and create package.I have deployed my package in vendor machine. I run the setup by enter into their local id and the setup got installed. But when the user login to their intranet(ie.LAN) it shows msdasql.dll...

DLL Initialization Failure [ 1 Answers ]

Hi, I've been having a terrible time ridding my work computer of Win.32 viruses, SpyBot trojans, Malware, etc. I've used NoAdware, Spyware Bot and XoftSpy programs with limited improvement. I still get the "System Alert: Trojan-SpyWin.32@mx balloon. Also, when I shutdown the computer, I get an...

DLL initialization failed [ 14 Answers ]

Whenever I shutdown windows, I get this message: Vrixvz.exe - DLL Initialization Failed - The application failed to initialize because the window station is shutting down. The message pops up quickly and disappears only during shutdown. Recently I updated my Windows from "Tools" option...

Simple convert in VB6 [ 1 Answers ]

I'm having a minor problem to convert hexadecimal (entered as string in a textbox) into decimal. Currently, I'm trying to do the conversion through ascii method... but seems unable to get the right syntax. Or are there any functions in VB6 that does this? Thanks everyone.


View more questions Search