954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article
delifion
Light Poster
35 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

aa a;
a.getAttribute();
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

The line:

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:

Id = aa->getAttribute();


What this does is assign the value returned by aa.getAttribute() to Id.

That should solve your problem..

Jas.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

Nope.

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;
}

Because getAttribute() returns the address of str if means you can assign variables to it using the = operator, as above.

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

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:

a* aa = new a();

aa->GetAttribute() = Id;


or as you've posted:

a aa;

aa.GetAttribute() = Id;


I stand corrected!

J.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

#include
#include

using namespace std;
int i=0, j=0, zero_fill, l=0;
class dec_to_bin {
public:
char bin[33], rev[33];
char *to_bin(unsigned int dec, int size);
char *to_bin(unsigned short int dec, int size);
};
char *dec_to_bin :: to_bin(unsigned int dec, int size) {
while(dec > 0) {
bin[i] = dec%2+48;
dec = dec/2;
i++;
}
zero_fill = size-i;
while(l

Nasas
Newbie Poster
7 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You