Call function from class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Call function from class

 
0
  #1
May 2nd, 2009
Hi, this is what i'm trying to do
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,868
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Call function from class

 
0
  #2
May 2nd, 2009
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
  1. aa a;
  2. a.getAttribute();
Last edited by twomers; May 2nd, 2009 at 9:24 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 314
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 56
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Call function from class

 
0
  #3
May 2nd, 2009
The line:
  1. aa.getAttribute() = Id;
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:
  1. 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!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,868
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Call function from class

 
0
  #4
May 2nd, 2009
Nope.
  1. class thing {
  2. private:
  3. std::string str;
  4.  
  5. public:
  6. thing(std::string _str)
  7. : str(_str) {
  8. }
  9. std::string &getAttribute() {
  10. return str;
  11. }
  12. };
  13. int main(int argc, char* argv[]) {
  14. thing inst("one");
  15. std::cout<< inst.getAttribute() << "\n";
  16.  
  17. inst.getAttribute() = "two";
  18. std::cout<< inst.getAttribute() << "\n";
  19.  
  20. thing *ptr = &inst;
  21. ptr->getAttribute() = "three";
  22. std::cout<< inst.getAttribute() << "\n";
  23.  
  24. return 0;
  25. }
Because 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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 314
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 56
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Call function from class

 
0
  #5
May 5th, 2009
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:
  1. a* aa = new a();
  2.  
  3. aa->GetAttribute() = Id;

or as you've posted:
  1. a aa;
  2.  
  3. 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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC