在 c++ 中编译复数类时出错
Errors when compiling complex number class in c++
我的代码应该显示复数的加法、减法等,而无需用户输入。我有三个类:test.cpp,complex.cpp和complex.h分别运行程序,定义构造函数和方法,并创建一个标头类。但是,当我运行代码时,我收到一系列错误,我已经尝试了一段时间。
复杂.h
//complex class definition
#ifndef COMPLEX_H
#define COMPLEX_H
//class complex
class Complex
{
public:
Complex(); //default no arg constructor
Complex(double a); //one arg constructor
Complex(double a, double b); //two arg constructor
Complex operator+(const Complex &) const; //addition method
Complex operator-(const Complex &) const; //subtraction method
Complex operator*(const Complex &) const; //multiplication method
Complex operator/(const Complex &) const; //division method
void print() const; //output
private:
double a; //real number
double b; //imaginary number
}; //end class Complex
#endif
复杂.cpp
#include "stdafx.h"
#include <iostream>
#include "complex.h"
using namespace std;
//no arg constructor
Complex::Complex()
{
a = 0;
b = 0;
}
//one arg instructor
Complex::Complex(double real)
{
a = real;
b = 0;
}
//two arg constructor
Complex::Complex(double real, double imaginary)
{
a = real;
b = imaginary;
}
//addition
Complex Complex::operator+(const Complex &number2) const
{
return a + number2.a, b + number2.b;
}
//subtraction
Complex Complex::operator-(const Complex &number2) const
{
return a - number2.a, b - number2.b;
}
//multiplication
Complex Complex::operator*(const Complex &number2) const
{
return a * number2.a, b * number2.b;
}
//division
Complex Complex::operator/(const Complex &number2) const
{
return a / number2.a, b / number2.b;
}
//output display for complex number
void Complex::print() const
{
cout << '(' << a << ", " << b << ')';
}
测试.cpp
#include <iostream>
#include <complex>
#include "complex.h"
#include "stdafx.h"
using namespace std;
int main()
{
Complex b(1.0, 0.0);
Complex c(3.0, -1.0);
/*cout << "a: ";
a.print();
system ("PAUSE");*/
};
现在在测试中,因为代码显示较低的部分被注释掉了,我试图只调用三个构造函数中的两个,看看我是否可以让其中任何一个工作。
我收到的错误:
error C2065: 'Complex' : undeclared identifier
error C2065: 'Complex' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'b'
error C2146: syntax error : missing ';' before identifier 'c'
error C3861: 'b': identifier not found
error C3861: 'c': identifier not found
我正在尝试在Visual Studio 2010中运行它。有人可以帮忙吗?
我认为问题出在您的复杂.cpp。如果在编译类的代码时出错,则该类是未知的,因此标识符也是复杂的。
快速查看后,一切似乎都是正确的,但您的运算符函数有一些错误:
不對:
//addition
Complex Complex::operator+(const Complex &number2) const
{
return a + number2.a, b + number2.b;
}
正确:
//addition
Complex Complex::operator+(const Complex &number2) const
{
return Complex(a + number2.a, b + number2.b);
}
问题是运算符方法的返回类型。它们必须与声明的相同。此外,您不能同时返回两个变量。如果要这样做,则必须使用结构或类(通过使用新值调用类 Complex 的构造函数)。
如果要操作成员 a 和 b,则不能将方法定义为"const"。如果你说一个函数是常量,这意味着所有类成员的值都不会通过调用此方法来操作。
您可能会遇到 stdafx.h 问题
至少你应该尝试把#include "stdafx.h
放在其他所有包含之前或您可以尝试将项目属性设置为不使用 stdafx.h
1. right click on project and select Properties
2. go to C/C++ -> Precompiled Headers
3. select "Not Using Precompiled Headers"
#include <complex>
#include "complex.h"
那是一种难闻的气味。这两个 #include 文件都可能对标头保护使用相同的名称。
尝试像这样改变你的。
#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H
..也许可以将您的模块从"复杂"重命名为其他名称,以避免与同名的系统头文件发生冲突。
尽量简单。然后重新添加零件,直到找到错误。
这行得通吗?
#include "complex.h"
int main()
{
Complex c(3.0, -1.0);
return 0;
};
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- PrintWindow函数在notepad.exe中出错