我一直在研究这个错误,找不到解决办法。我有一个带有虚函数 compute() 的基类 Convert,以及一个派生类 L_To_G(升到加仑)。在我的 main.cpp 文件中,声明:
p = &L_To_G(num);
给我以下错误:
../main.cpp:在函数'int main()'中:
../main.cpp:37:20: 错误:获取临时地址 [-fpermissive] p = &L_To_G(num);
头文件中的代码:
class Convert
{
protected:
double val1, val2; //data members
public:
Convert(double i) //constructor to initialize variable to convert
{
val1 = i;
val2 = 0;
}
double getconv() //accessor for converted value
{
return val2;
}
double getinit() //accessor for the initial value
{
return val1;
}
virtual void compute() = 0; //function to implement in derived classes
virtual ~Convert(){}
};
class L_To_G : public Convert //derived class
{
public:
L_To_G(double i) : Convert(i) {}
void compute() //implementation of virtual function
{
val2 = val1 / 3.7854; //conversion assignment statement
}
};
main.cpp 文件中的代码:
int main()
{
...
switch (c)
{
case 1:
{
p = &L_To_G(num);
p->compute();
cout << num << " liters is " << p->getconv()
<< " gallons " ;
cout << endl;
break;
}
...