c++ - 错误: expected primary-expression before ‘double’

标签 c++ syntax-error

#include <iostream> 
#include <complex>

using namespace std;

class MyComplex{
  private:
    int real, img;

  public:
    MyComplex();
    MyComplex(int,int);
    ~MyComplex();
    void set(int,int);
    void display();
};

MyComplex::MyComplex(){
  cout << "i'm being constructed (default).\n";
  real=img=0;
}

MyComplex::MyComplex(int r, int i){
  cout << "i'm being constructed (parameterized).\n";
  real=r;
  img=i;
}

MyComplex::~MyComplex(){
  cout << "I'm being destroyed\n";
}

void MyComplex::set(int r, int i){
  real=r;
  img=i;
}

void MyComplex::display(){
  cout << real << "+i" << img << endl;
}



int main(){
  MyComplex c1;
  MyComplex c2(10,-8);
  c1.set(2,9);
  c1.display();
  c2.display();

  cout << "Magnitude"<< double abs(const complex) << endl;
}

第一次来这些论坛,如果我的代码写得很糟糕,我深表歉意,我只是一个初学者。

我在一本书上发现了一个作业,要求你计算复数的大小。

我收到此错误:

testcomplex.cpp: In function ‘int main()’:
testcomplex.cpp:50:25: ***error: expected primary-expression before ‘double’
   cout << "Magnitude"<< double abs(const complex) << endl;***

最佳答案

您似乎已经编写了函数的声明而不是调用它。假设您实际上有一个名为 abs 的函数,您只需在括号中将变量传递给它即可调用它:

cout << "Magnitude " << abs(c1) << endl;

关于c++ - 错误: expected primary-expression before ‘double’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872256/

相关文章:

c++ - 解决简单哈希表中的内存泄漏和语法问题

loops - Ansible 2.7.5 |错误! 'set_fact'不是Play的有效属性

sqlite - 在 SQLite 中使用 id 选择多行

java - Jython SyntaxError(来自 Java 应用程序)

c# - 无法调用表单和类,无法使用Properties.Settings

javascript - 未捕获的ReferenceError : load is not defined

c++ - 从二进制文件中读取 unsigned long

c++ - 如何在 QT Creator 中手动运行 CMake 和运行

c++ - 用方括号 [] 初始化 std::vector<int> ;怎么了?

c++ - 如何使用omnet++从两个节点之间的 `Link`读取值