0

In below code while compiling i am getting error , Error : main.cpp:8:57: error: expected type-specifier before ‘Staircase’ std::unique_ptr algodiagnostic (new Staircase());

compilation command g++ HardwareDiagnostic.cpp HardwareDiagnostic.h main.cpp -std=c++0x -o res

it works fine if compile entire code in single file without creating header file and main.cpp separately. Can anybody suggest how to fix this issue.
//HardwareDiagnostic.h//

    #ifndef HARDWAREDIAGNOSTIC_H_
    #define HARDWAREDIAGNOSTIC_H_
    #include <iostream>
    #include <memory>
    using namespace std;
    class HardwareDiagnostic
    {

    public:
    virtual bool checkPort();
    virtual void startDiagnostic();
    virtual int publishAlgoDiagnosticInfo();
    virtual void clearErrorStatus(){cout<<"Algorithm Diagnostics"<<endl;}
    virtual ~HardwareDiagnostic() {cout<<"calling virtual destructor"<<endl;}
    };

    #endif /* HARDWAREDIAGNOSTIC_H_ */

//HardwareDiagnostic.cpp//

#include"HardwareDiagnostic.h"
class Localization : public HardwareDiagnostic
{

public:
Localization() { cout << "calling Localization constructor";}

bool checkPort(){cout<<"checkport :Localization";}
void startDiagnostic(){cout<<"start Diagnostic:Localization";}
int publishAlgoDiagnosticInfo() {cout<<"publish Diagnostic:Localization";}
void clearErrorStatus(){ cout<<"Localization:publish Diagnostic";}
~Localization () { cout<<"calling Localization destructor ";}

};

class Staircase : public HardwareDiagnostic
{

public:
Staircase () {cout<<"Staircase constructor";}
bool checkPort(){cout<<"Staircase";}
void startDiagnostic(){cout<<"StairCase:start Diagnostic";}
int publishAlgoDiagnosticInfo() {cout<<"StairCase:publish Diagnostic";}
void clearErrorStatus(){ cout<<"staircase:publish Diagnostic";}
~Staircase(){cout<<"calling Staircase destructor";}
};

//main.cpp//
 #include "HardwareDiagnostic.h"
using namespace std;
int main() {

std::unique_ptr<HardwareDiagnostic> algodiagnostic (new Staircase());//getting error in this line
return 0;

}
2
Contributors
1
Reply
13
Views
2 Months
Discussion Span
Last Post by Coller
1

Many warnings and mistakes in your code (not saying this to put you down or anything) so I will try to take it step by step.

Firstly, you should define your classes in your header (.h) files. That is, move your two derived classes ('Staircase' and 'Localization') definitions into the same header file as the base class ('HardwareDiagnostic') OR create seperate header files for them. The first way of doing things is easier but the second one is (sometimes) much cleaner and easier to refactor later on.

So your HardwareDiagnostic.h would be like this:

#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_

#include <iostream>

class HardwareDiagnostic
{
    public:
        virtual bool checkPort() = 0;
        virtual void startDiagnostic() = 0;
        virtual int publishAlgoDiagnosticInfo() = 0;

        virtual void clearErrorStatus() {
            std::cout << "Algorithm Diagnostics" << std::endl;
        }
        virtual ~HardwareDiagnostic() {
            std::cout << "calling BASE virtual destructor" << std::endl;
        }
};

class Localization : public HardwareDiagnostic
{
    public:
        Localization() {
            std::cout << "calling Localization constructor";
        }
        bool checkPort() {
            std::cout << "checkport :Localization";
        }
        void startDiagnostic() {
            std::cout << "start Diagnostic:Localization";
        }
        int publishAlgoDiagnosticInfo() {
            std::cout << "publish Diagnostic:Localization";
        }
        void clearErrorStatus() {
            std::cout << "Localization:publish Diagnostic";
        }
        ~Localization () {
            std::cout << "calling Localization destructor ";
        }
};

class Staircase : public HardwareDiagnostic
{
    public:
        Staircase () {
            std::cout << "Staircase constructor";
        }
        bool checkPort() {
            std::cout << "Staircase";
        }
        void startDiagnostic() {
            std::cout << "StairCase:start Diagnostic";
        }
        int publishAlgoDiagnosticInfo() {
            std::cout << "StairCase:publish Diagnostic";
        }
        void clearErrorStatus() {
            std::cout << "staircase:publish Diagnostic";
        }
        ~Staircase() {
            std::cout << "calling Staircase destructor";
        }
};

#endif /* HARDWAREDIAGNOSTIC_H_ */

And your HardwareDiagnostic.cpp would just include his header file and be empty after.

Now your main would run the line of code just fine:

#include "HardwareDiagnostic.h"

#include <memory>

int main()
{
    std::unique_ptr<HardwareDiagnostic> algodiagnostic (new Staircase());
    return 0;
}

There are, though, a series of problems with your code. You're trying to derive from a base class, that, by the looks of your initial code, hasn't an implementation (it should've been present in the cpp file, but it isn't). So I guess, what you want to do, is to declare the first functions as pure virtual functions. This will prevent you from creating an instance of the base class, by the way. Also return from functions when you have different return values than void (see the warnings from compiler) and stop using 'using namesapce std' as it's a bad practice, that pollutes the global namespace. About the smart pointers, you could just use: auto algodiagnostic = std::make_unique<Staircase>(); since it's
(nearly) the same things and it's shorter / cleaner.

In the end, this is how I would code these classes (Note: I would separate the derived classes in separate headers. Now, for pure example purposes, I just cram them into a single header file):

HardwareDiagnostic.h:

#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_

struct HardwareDiagnostic
{
    HardwareDiagnostic() = default;
    virtual ~HardwareDiagnostic();

    virtual bool checkPort() const = 0;
    virtual void startDiagnostic() const = 0;
    virtual int publishAlgoDiagnosticInfo() const = 0;

    virtual void clearErrorStatus() const;
};

struct Localization : public HardwareDiagnostic
{
    Localization();
    ~Localization();

    virtual bool checkPort() const override;
    virtual void startDiagnostic() const override;
    virtual int publishAlgoDiagnosticInfo() const override;
    virtual void clearErrorStatus() const override;
};

struct Staircase : public HardwareDiagnostic
{
    Staircase();
    ~Staircase();

    virtual bool checkPort() const override;
    virtual void startDiagnostic() const override;
    virtual int publishAlgoDiagnosticInfo() const override;
    virtual void clearErrorStatus() const override;
};

#endif /* HARDWAREDIAGNOSTIC_H_ */

HardwareDiagnostic.cpp:

#include "HardwareDiagnostic.h"

#include <iostream>

void HardwareDiagnostic::clearErrorStatus() const
{
    std::cout << "Algorithm Diagnostics" << std::endl;
}

HardwareDiagnostic::~HardwareDiagnostic()
{
    std::cout << "calling BASE virtual destructor" << std::endl;
}

Localization::Localization()
{
    std::cout << "calling Localization constructor" << std::endl;
}

bool Localization::checkPort() const
{
    std::cout << "checkport :Localization" << std::endl;
    return true; // just a dummy return (to be implemented)
}

void Localization::startDiagnostic() const
{
    std::cout << "start Diagnostic:Localization";
}

int Localization::publishAlgoDiagnosticInfo() const
{
    std::cout << "publish Diagnostic:Localization" << std::endl;
    return 0; // just a dummy return (to be implemented)
}

void Localization::clearErrorStatus() const
{
    std::cout << "Localization:publish Diagnostic" << std::endl;
}

Localization::~Localization()
{
    std::cout << "calling Localization destructor " << std::endl;
}

Staircase::Staircase()
{
    std::cout << "Staircase constructor" << std::endl;
}

bool Staircase::checkPort() const
{
    std::cout << "Staircase" << std::endl;
    return true; // just a dummy return (to be implemented)
}

void Staircase::startDiagnostic() const
{
    std::cout << "StairCase:start Diagnostic" << std::endl;
}

int Staircase::publishAlgoDiagnosticInfo() const
{
    std::cout << "StairCase:publish Diagnostic" << std::endl;
    return 0; // just a dummy return (to be implemented)
}

void Staircase::clearErrorStatus() const
{
    std::cout << "staircase:publish Diagnostic" << std::endl;
}

Staircase::~Staircase()
{
    std::cout << "calling Staircase destructor" << std::endl;
}

main.cpp:

#include "HardwareDiagnostic.h"

#include <memory>

int main()
{
    auto algodiagnostic = std::make_unique<Staircase>();
    return 0;
}
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.