在我的c ++程序中,我需要创建一个名为ComplexNumber的类,它包含复数的信息。我已经在头文件中编写了所有函数,并将函数移到.cpp文件中。
我当前程序的atof函数无法正常工作。它显示一个错误,声明atof不在标准中。当我删除代码的std ::部分时,错误显示atof未在范围中声明。
以下是两个示例错误消息:
ComplexNumber.cpp:63:33:错误:在此范围内未声明'atof' o.realPart = atof(temp.c_str); ^ ComplexNumber.cpp:66:23:错误:'atof'不是'std'的成员 o.imaginaryPart = std :: atof(temp.c_str);
错误来自我的ComplexNumber.cpp文件,说实话我不太了解c ++来确定如何修复这样的事情。任何帮助将不胜感激。
的main.cpp
/*
* File: main.cpp
* Author: Tyler Weaver
*
* Created on February 2, 2015, 4:08 PM
*/
#include <cstdlib>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
return 0;
}
ComplexNumber.h
/*
* File: ComplexNumber.h
* Author: Tyler Weaver
*
* Created on February 2, 2015, 4:10 PM
*/
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H
#include <cmath>
#include <ostream>
#include <istream>
#include <string>
class ComplexNumber {
public:
ComplexNumber(float real = 0, float imaginary = 0);
ComplexNumber operator+(ComplexNumber o);
ComplexNumber operator-(ComplexNumber o);
ComplexNumber operator*(ComplexNumber o);
ComplexNumber operator/(ComplexNumber o);
bool operator==(ComplexNumber o);
friend std::ostream& operator<<(std::ostream& out, ComplexNumber o);
friend std::istream& operator>>(std::istream& in, ComplexNumber o);
double getMagnitude();
private:
float realPart;
float imaginaryPart;
};
#endif /* COMPLEXNUMBER_H */
ComplexNumber.cpp
/*
* File: ComplexNumber.cpp
* Author: Tyler Weaver
*
* Created on February 2, 2015, 4:10 PM
*/
#include "ComplexNumber.h"
ComplexNumber::ComplexNumber(float real, float imaginary)
{
realPart = real;
imaginaryPart = imaginary;
}
ComplexNumber ComplexNumber::operator+(ComplexNumber o)
{
return ComplexNumber(realPart + o.realPart,
imaginaryPart + o.imaginaryPart);
}
ComplexNumber ComplexNumber::operator-(ComplexNumber o)
{
return ComplexNumber(realPart - o.realPart,
imaginaryPart - o.imaginaryPart);
}
ComplexNumber ComplexNumber::operator*(ComplexNumber o)
{
return ComplexNumber(realPart * o.realPart -
imaginaryPart * o.imaginaryPart,
realPart * o.imaginaryPart + imaginaryPart * o.realPart);
}
ComplexNumber ComplexNumber::operator/(ComplexNumber o)
{
double divisor = o.realPart * o.realPart +
o.imaginaryPart * o.imaginaryPart;
return ComplexNumber((realPart * o.realPart +
imaginaryPart * o.imaginaryPart) / divisor,
(imaginaryPart * o.realPart - realPart * o.imaginaryPart) /
divisor);
}
bool ComplexNumber::operator==(ComplexNumber o)
{
return ((realPart == o.realPart) &&
(imaginaryPart = o.imaginaryPart));
}
std::ostream& operator<<(std::ostream& out, ComplexNumber o)
{
out << o.realPart << " + " << o.imaginaryPart << "i\n";
return out;
}
std::istream& operator>>(std::istream& in, ComplexNumber o)
{
std::string temp;
getline(in, temp, '+');
o.realPart = std::atof(temp.c_str);
getline(in, temp, 'i');
o.imaginaryPart = std::atof(temp.c_str);
return in;
}
double ComplexNumber::getMagnitude()
{
return sqrt(realPart * realPart +
imaginaryPart * imaginaryPart);
}
答案 0 :(得分:2)
您忘记了在ComplexNumber.cpp中#include <cstdlib>
,atof
函数在该标头中声明。
在C ++中,源文件是相互独立编译的,所有要使用的函数都应在某处声明。如果它们在单独的头文件中声明,则在引用要调用的函数之前应包含该文件。
答案 1 :(得分:1)
您需要包含相应的标题:
#include <cstdlib>
将其放在ComplexNumber.cpp