Friday 15 April 2011

c++ - error: "null" was not declared in this scope -



c++ - error: "null" was not declared in this scope -

i @ wits end trying debug above error. providing code below assist troubleshooting process:

#include <string> using namespace std; class packet { int index; string content; public: packet() { index = null; content = null; } packet(int i, string data) { index = i; content = data; } void setindex(int i) {index = i;} void setcontent(string input) {content = input;} int getindex() {return index;} string getcontent() {return content;} };

i have tried adding #include <cstdlib> (from here), #include <cstddef>, , manually defining null. nil has succeeded. if can shed lite on missing, appreciated.

you want write:

packet() : index(0), content("") { } packet(int i, string data) : index(i), content(data) { }

the word null not reserved c++ standard. null null pointer constant, not appropriate initializer int (you might away it, equivalent writing 0, , bad practice utilize null mean 0 or '\0'), , not appropriate initialize string null either.

you use:

packet(int = 0, string info = "") : index(i), content(data) { }

to have single constructor defaulted arguments.

c++ pointers null nodes singly-linked-list

No comments:

Post a Comment