g++ error - expected unqualified-id before ')&

2019-07-11 14:05发布

问题:

When I go to compile this code it says it expected an unqualified-id before the ) in my constructor

analysis2.h:

#ifndef _ANALYSIS2_H
#define _ANALYSIS2_H

class Analysis2{

public:

    Analysis2();
...

analysis2.cpp:

#include "analysis2.h"

using namespace std;

Analysis2()
{
    Seconds_v = 0;
    Seconds_t = 0;
}
...

How do I fix this?

回答1:

In analysis2.cpp you need to tell the compiler that you are defining the constructor by giving it a scope:

Analysis2::Analysis2()
{
    Seconds_v = 0;
    Seconds_t = 0;
}

Scope Resolution Operator



回答2:

In analysis2.cpp, write this:

Analysis2::Analysis()
{
    Seconds_v = 0;
    Seconds_t = 0;
}

You have to include the class name (Analysis2::).



回答3:

Type

Analysis2::

before the method name or constructor/destructor



回答4:

You need to specify Analysis2::Analysis2() if you are trying to define a constructor. Otherwise, the compiler supposes that the Analysis2 is the name of a type in a declaration of something else.