【问题标题】:error: expected primary-expression before ‘double’错误:“双”之前的预期主表达式
【发布时间】:2015-03-05 07:27:39
【问题描述】:
#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(complex)吗?
  • double 在那里做什么?!
  • double abs(const complex) 完全没有意义。它想做什么?
  • 您正在尝试打印函数声明。这没有多大意义。

标签: c++ syntax-error


【解决方案1】:

您似乎编写了函数的声明而不是调用它。假设您实际上有一个名为 abs 的函数,您可以通过在括号中传递一个变量来调用它:

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

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 2023-04-11
    • 2019-09-23
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多