| | |
default variable as a pointer in class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
the title describes the problem. here's how the code looks like:
g++ returns:
i don't want to use static... do you know what is the proper way of coding it?
C++ Syntax (Toggle Plain Text)
#include <cstdio> class example { public: class node { public: int value; node *left, *right; }; node *root; example (int a) { root=new node; root->value=a; } // HERE'S THE PROBLEM: void function (node *variable=root) { } }; int main() { }
g++ returns:
C++ Syntax (Toggle Plain Text)
p.cpp:13: error: invalid use of non-static data member 'przyklad::root' p.cpp:21: error: from this location
i don't want to use static... do you know what is the proper way of coding it?
Here is an explaination of default parameter values. That link talks about exactly what you are trying to do.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
i cannot find the answer there. i have no problem in using default parameters, except this particular case.
may it be somewhat connected to the fact, that if i put there
i get
a correct code would be highly appreciated
may it be somewhat connected to the fact, that if i put there
C++ Syntax (Toggle Plain Text)
void function (node *variable=this->root)
C++ Syntax (Toggle Plain Text)
p.cpp:21: error: 'this' may not be used in this context
>> cannot find the answer there.
Click on the link "Restrictions on Default Arguments" in the left pannel.
>>a correct code would be highly appreciated
The only correct way to do that is to make the local variable static.
Click on the link "Restrictions on Default Arguments" in the left pannel.
•
•
•
•
You cannot use local variables in default argument expressions. For example, the compiler generates errors for both function g() and function h() below:
C++ Syntax (Toggle Plain Text)
void f(int a) { int b=4; void g(int c=a); // Local variable "a" cannot be used here void h(int d=b); // Local variable "b" cannot be used here }
The only correct way to do that is to make the local variable static.
Last edited by Ancient Dragon; Apr 20th, 2008 at 12:44 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- access Windows Address Book (C)
- fstream Tutorial (C++)
- default constructor&class (C++)
- C++ .NET Character class errors (C++)
- Query regarding the 'this' operator or pointer? (C++)
- Another C++ N00B Begging For Help (C++)
- Abstract Class member function problem (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Assignment NEED HELP URGENTLY
- Next Thread: MFC edit control text
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






