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?

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();

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.

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.

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.

#include <cstdlib>
#include <iostream>


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 = dec%2+48;
dec = dec/2;
i++;
}
zero_fill = size-i;
while(l<zero_fill) {
bin[i+l] = '0';
l++;
}
while(j < i+l) {
rev[j] = bin[i+l-j-1];
j++;
}
rev[j] = 0;
return rev;
}



char *dec_to_bin :: to_bin(unsigned short int dec, int size) {
to_bin(dec,size);   //using same function again
}
int main(int argc, char *argv[])
{


unsigned int num = 20;
unsigned short int num1 = 21;
dec_to_bin b;
printf("bin int: %s\n", b.to_bin(num, 32));   //works fine
printf("bin short: %s\n", b.to_bin(num1, 16)); //program breakes down
system("PAUSE");
return EXIT_SUCCESS;
}

Can u help me guys. I have problem of calling function from class and overloading. When I'm trying to print short int type number, the program brakes down. No errors are shown which tell me whats wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.