| | |
Call function from class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
Hi, this is what i'm trying to do
above is the class, i want to call function getAttribute() from
class a is a linked list: list<a*> aa
this is the error message:
In function `void sortFile(Manager& manage, const char* file)':
`getPacketID' has not been declared
request for member of non-aggregate type before '(' token
what did I do wrong?
class a
{
private:
string attribute;
string p_msg;
public:
string& getAttribute()
{
return attribute;
}
};above is the class, i want to call function getAttribute() from
class a is a linked list: list<a*> aa
void sortFile(Manager& manage, const char* file)
{
string text, Id;
text = file;
Id = text.substr (0, 2);
a* aa; //i'm trying to make object of class a >.<
aa.getAttribute() = Id; //this line doesn't work, I'm trying to assign
attribute from getAttribute() to var Id.
}this is the error message:
In function `void sortFile(Manager& manage, const char* file)':
`getPacketID' has not been declared
request for member of non-aggregate type before '(' token
what did I do wrong?
aa is a pointer so you have to either dynamically allocate an instance of a, or set it to the address of an already instantiated instance, and call
aa->getAttribute();. Alternatively do C++ Syntax (Toggle Plain Text)
aa a; a.getAttribute();
Last edited by twomers; May 2nd, 2009 at 9:24 am.
The line:
Is where you're going wrong here....
The assignment operator assigns the left hand operand the value of the right operand.
So you are trying to assign the value of id to the value of the getAttribute function....Which is nonsense.
What you really want is this:
What this does is assign the value returned by aa.getAttribute() to Id.
That should solve your problem..
Jas.
c++ Syntax (Toggle Plain Text)
aa.getAttribute() = Id;
The assignment operator assigns the left hand operand the value of the right operand.
So you are trying to assign the value of id to the value of the getAttribute function....Which is nonsense.
What you really want is this:
c++ Syntax (Toggle Plain Text)
Id = aa->getAttribute();
What this does is assign the value returned by aa.getAttribute() to Id.
That should solve your problem..
Jas.
Last edited by JasonHippy; May 2nd, 2009 at 9:28 am. Reason: typo...
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
Nope.
Because
C++ Syntax (Toggle Plain Text)
class thing { private: std::string str; public: thing(std::string _str) : str(_str) { } std::string &getAttribute() { return str; } }; int main(int argc, char* argv[]) { thing inst("one"); std::cout<< inst.getAttribute() << "\n"; inst.getAttribute() = "two"; std::cout<< inst.getAttribute() << "\n"; thing *ptr = &inst; ptr->getAttribute() = "three"; std::cout<< inst.getAttribute() << "\n"; return 0; }
getAttribute() returns the address of str if means you can assign variables to it using the = operator, as above. Last edited by twomers; May 2nd, 2009 at 9:30 am.
Aha, yes sorry...I was in a bit of a rush on Saturday....I'd started replying to the thread and then my other half started harassing me 'cause she wanted me to drive her into town...In my haste, I misread the original post..Yes I see now..
I guess I should've cancelled my reply and waited until I had time to look at it properly!
Yes, the OP either needed to do this:
or as you've posted:
I stand corrected!
J.
I guess I should've cancelled my reply and waited until I had time to look at it properly!
Yes, the OP either needed to do this:
C++ Syntax (Toggle Plain Text)
a* aa = new a(); aa->GetAttribute() = Id;
or as you've posted:
C++ Syntax (Toggle Plain Text)
a aa; aa.GetAttribute() = Id;
I stand corrected!
J.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
![]() |
Similar Threads
- stuck on a member function for a class (C++)
- call function in class module (VB.NET)
- How do I declare a class member function in another class? (Python)
- how to call a function in href? (ASP.NET)
- Call Function (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: press any key to continue
- Next Thread: plz help!
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






