C++非静态成员引用必须相对于特定对象
C++ Nonstatic member referencing must be relative to specific object
这个复数程序应该从txt文档中获取三个参数,第一个参数指示后面的两个参数是极坐标形式还是矩形形式的数字,并输出以矩形和极坐标形式给出的每个复数。这里显示了头文件和源代码。txt文档的格式如下:
p 50 1.2
r 4 0.8
r 2 3.1
p 46 2.9
p 3 5.6
如果没有在类声明中将int inputfile()函数声明为静态,则生成会给出错误"非法调用非静态成员函数"。
通过函数的静态声明(如下所示),构建在函数定义inputfile()内引用类成员Pfirst、Psecond、Rfirst和Rsecond时会出现错误,即"对非静态成员的非法引用"。
成员声明也不能变为静态的,因为类将无法初始化构造函数中的参数。
如何绕过这个"静态"问题?
#define Complex_h
class Complex
{
char indicator;
const double pi;
public:
double Pfirst, Psecond, Rfirst, Rsecond;
Complex(char i = 0, double Pf = 0, double Ps = 0, double Rf = 0, double Rs = 0, const double pi = 3.14159265) // with default arguments (= 0)
: indicator(i), Pfirst(Pf), Psecond(Ps), Rfirst(Rf), Rsecond(Rs), pi(pi) {}
~Complex();
void poltorect();
void recttopol();
static int inputfile();
};
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include "Complex.h"
using namespace std;
int Complex::inputfile()
{
ifstream ComplexFile;
ComplexFile.open("PolarAndRectangular.txt");
string TextArray[3];
string TextLine;
stringstream streamline, streamfirst, streamsecond;
while (getline(ComplexFile,TextLine))
{
streamline << TextLine;
for (int j=0; j<3; j++)
{streamline >> TextArray[j];}
streamline.str("");
streamline.clear();
if (TextArray[0] == "r")
{
streamfirst << TextArray[1];
streamfirst >> Rfirst;
streamsecond << TextArray[2];
streamsecond >> Rsecond;
cout << "Complex number in rectangular form is " << Rfirst << "," << Rsecond << endl;
void recttopol();
cout << "Complex number in polar form is " << Pfirst << "," << Psecond << endl;
}
else
{
streamfirst << TextArray[1];
streamfirst >> Pfirst;
streamsecond << TextArray[2];
streamsecond >> Psecond;
cout << "Complex number in polar form is " << Pfirst << "," << Psecond << endl;
void poltorect();
cout << "Complex number in rectangular form is" << Rfirst << "," << Rsecond << endl;
}
streamfirst.str("");
streamfirst.clear();
streamsecond.str("");
streamsecond.clear();
}
ComplexFile.close();
system("pause");
return 0;
}
void Complex::recttopol()
{
Pfirst = sqrt((Rfirst*Rfirst)+(Rsecond*Rsecond));
Psecond = (atan(Rsecond/Rfirst))*(pi/180);
}
void Complex::poltorect()
{
Rfirst = Pfirst*(cos(Psecond));
Rsecond = Pfirst*(sin(Psecond));
}
int main()
{
Complex::inputfile();
system("pause");
return 0;
}
您忘记创建类型为Complex
的对象。
使您的inputfile()
方法非静态并执行:
int main()
{
Complex complex; // Object construction.
complex.inputfile();
system("pause");
return 0;
}
相关文章:
- 如何仅使用对象名称打印特定于对象的成员
- 非静态成员引用必须相对于特定对象
- 如何在3D世界中获得对象相对于父母的绝对位置
- 不确定如何修复;非静态成员参考必须相对于特定对象
- 相对于向量对象的两个成员,找到两个向量的相交的有效方法
- 请澄清术语类静态对象(相对于类静态成员)
- C++标准库容器相对于所包含对象的线程安全性
- C++非静态成员引用必须相对于特定对象
- 无法理解错误 msg:"非静态成员引用必须相对于特定对象"
- 为什么我的 OpenGL 对象相对于最后一个绘制的对象绘制
- 新的新类对象的函数;错误:非静态成员引用必须相对于特定对象
- C++非静态成员引用必须相对于特定对象
- 我是否需要使用继承对象(相对于基对象)覆盖我的虚函数?
- lambda对象相对于函数指针转换的生命周期
- 在另一个类内部构造一个类(非静态成员引用必须相对于特定对象)
- 非静态成员引用必须相对于特定的c++对象单例类
- 错误:非静态成员引用必须相对于特定对象
- C++接口错误,非静态成员引用必须相对于特定对象
- C/C++.库相对于组合对象文件的优势
- 删除std::lock_guard相对于其他堆栈分配对象的顺序/速度