tags:

views:

190

answers:

5

Hi all, I have just started to practice c++ and I am stuck at one point. I have a Node class and the class has a constructor like this:

class Node
{
    public:
          Node(std::string,Node *,int,int,int,int);
    private:
          std::string state;
          Node* parent_node;
          int total_cost;
          int path_cost;
          int heuristic_cost;
          int depth;  
}

Node::Node(std::string state,Node *parent_node,int path_cost,int heuristic_cost,int total_cost,int depth)
{
 this->state=state;
 this->parent_node=parent_node;
 this->path_cost=path_cost;
 this->heuristic_cost=heuristic_cost;
 this->total_cost=total_cost;
 this->depth=depth;
}

Everything works ok so far, but I can not create a Node object with a NULL parent_node. I have tried this:

Node *n = new Node("state name",NULL,0,15,20,1);

I have also tried creating a new object and assigning it as parent_node, but no success either.

Node *temp = new Node();
Node *n = new Node("state name",temp,0,15,20,1);

I am doing something wrong with the pointer but I don't know what I am missing. I get a compile error which says no matching function call.

Thanks in advance

A: 

What environment are you working in? Compiler version?

Andrew
Hi,I am working on Linux.g++ 4.3.3
Ahmet Keskin
This is a comment, not an answer.
GMan
You mean this should be a comment, not an answer.
SLaks
+6  A: 

Is your constructor public?

class Node
{
public:
    Node(std::string,Node *,int,int,int,int);
private:
    std::string state;
    Node* parent_node;
    int total_cost;
    int path_cost;
    int heuristic_cost;
    int depth;  
};


Also, don't forget the semicolon after Node declaration, this is not Java ;)

AraK
Well picked, AraK.
Andrew
Thanks Andrew I forgot the semicolon :)
AraK
Hi,Yes my constructor is public. I better correct that...
Ahmet Keskin
@sth Sorry for the mistak :)
AraK
+1  A: 

I think it's normal to specify 0 for a null pointer value, instead of NULL (NULL is a macro used for C which, depending on how the macro is defined, might not work well with C++).

Also, I don't like to see std::string as a parameter type. I'd prefer to see either const char*, or const std::string&.

Also Node* node = new Node(); will produce that compiler error, because when you declare a non-default constructor then you hide the default constructor (where by 'default constructor' I mean the constructor which takes no parameters). If you want to support new Node(); then you need to explicitly declare the default constructor (and, define how it's implemented.)

ChrisW
`NULL` is typically `#defined` to a plain `0` in C++ and to `((void*)0)` in C.
Adam Rosenfield
Yes, and if it were defined as `((void*)0)` that wouldn't work with C++. The OP didn't say what it's defined as in his code.
ChrisW
You should never define it yourself. Use the definitions provided in the standard headers.
Martin York
Stroustrup prefers to avoid using NULL: http://www.research.att.com/~bs/bs_faq2.html#null
ChrisW
Thank you for the tip
Ahmet Keskin
+1  A: 

This works fine for me on GCC-4 on Mac OS X, after correcting the typos and implementing a Node::Node() constructor. It'd probably be helpful if you edited your question to include all the code to your test program, as well as the actual error messages you're getting when you compile.

Mark Bessey
hi, sorry but the compiler gives the error message in turkish, which does not make any sense to me. someone translated the error messages into turkish very poorly.
Ahmet Keskin
Okay, but you can read English, right? Just install a stock version of GCC, and try reading that error message.
Mark Bessey
A: 

The following compiles fine for me:

// Added this.
#include <string>


class Node
{    public:
          Node(std::string,Node *,int,int,int,int);
    private:
          std::string state;
          Node* parent_node;
          int total_cost;
          int path_cost;
          int heuristic_cost;
          int depth;
}; // Added semicolon here

Node::Node(std::string state,Node *parent_node,
           int path_cost,int heuristic_cost,int total_cost,int depth)
{
        this->state=state;
        this->parent_node=parent_node;
        this->path_cost=path_cost;
        this->heuristic_cost=heuristic_cost;
        this->total_cost=total_cost;
        this->depth=depth;
}

// Put code inside main.
int main()
{
    Node *n = new Node("state name",NULL,0,15,20,1);
}
Martin York