tags:

views:

352

answers:

2

Hi,

i was curious to know why the following throws an error in g++ (cannot call member function without object). I suppose a workaround would be to have the B class variable as static variable in A - but i was curious to find out why, when there is an instance of A's child class C created, this still throws an error - many thanks!

#include <iostream>
#include <cstring>

using namespace std;


class B {
public:
  double var;

public:
  friend class A; 
  B() : var(1) { };
  void set(double new_rate);
};


class A {
protected:
   B main_B;

public:
  virtual void set_rate(double new_rate) { cout << "test"; 
   //B.set(new_rate); 
  }

};



class C : public A {

};

/*
void B::set(double new_rate) {
  var = new_rate;
  cout << "worked " <<current_rate <<endl;
}

*/



int main() {

  C test_C;
  A::set_rate ( 2.00 );
  return 0;
}
+6  A: 

Firstly,

C test_c();

does not create an instance of C, it declares a function that returns a C. You mean:

C test_c;

Secondly, non-static member functions can only be called on a specific instance of a class. So with the corrected code, you could say:

test_c.set_rate( 2.0);
anon
C test_c(); is as valid as C test_c; both create a variable. () means that the ctor has no parmaters. -1 until proven otherwise.
the_drow
thanks, corrected the instanciation.so methods cant be call the parent class (say, if its pure virtual) only on a specific instance (of a child class)?
v_a_bhatia
@the_drow I don't need to prove anything - you need to read book on C++.
anon
the_drow i think you are correct, it woudl use the default constructor, but was irrelevant in this case so made the code simpler..
v_a_bhatia
@the_drow: `C test_c();` is a function declaration, not an object declaration (and definition). I suggest you remove your -1; Neil is right.
Charles Bailey
He isn't correct.
anon
Just checked it, and he is correct. No idea why though.I tried to remove the downvote but it can't be removed unless the question is edited.
the_drow
The reason why is that both C and C++ have the rule that anything that can be parsed as a declaration will be so parsed. This looks like a function declaration, so that's what the parser thinks it is.
anon
@the_drow:Google for "most vexing parse" and you should find quite a bit about this.
Jerry Coffin
A: 

You can use an explicit <class>:: to call a non-static member function, thereby disabling any virtual function mechanism, but for a non-static member you still need to specify a class instance on which to call the function.

e.g.

int main()
{
    C test_C;
    test_C.A::set_rate(2.00);
    return 0;
}
Charles Bailey
Why the use of the A:: scope specification?
anon
I don't think that there is a good reason in this instance - tbh, I wasn't really sure exactly what the OP needed - but looking at the commented out code it might be something that OP is experimenting with.
Charles Bailey
had seen something similar, and was pretty sure it was done without satic, so trying to figure out how!
v_a_bhatia