1

myMain.cpp:

#include <memory>
#include "myClass.h"

static std::unique_ptr<myClass> classPtr; // Error thrown here
...

I am initializing in global scope because loading all the data into the properties of this class takes a while, so I'd like to do it once and have that data persist until I give an explicit command to delete it (classPtr.reset(nullptr)).

When I try to compile this: g++ myMain.cpp -o myMain.o I get: error: expected initializer before '<' token.

Why am I getting this error?

I've defined myClass in myClass.h and myClass.cpp; I think the error has to do with the constructor. I've simplified code & included only the important lines below.

myClass.h:

class myClass {
    std::string dataPath;
    std::vector<double> data;
public:
    myClass(std::string P = "./path/to/data-file.csv");
    ~myClass() {}
    const double findPercentile(double percentile = 0.0);
}

EDIT: Following tip from @FrançoisAndrieux I have fixed my constructor.

myClass.cpp:

myClass::myClass(const std::string P) : 
            dataPath(P) {
    // read data-sets into class member variables
}
1
  • Note related to your question : you declare your constructor myClass(std::string) but implement it as myClass(const std::string). You define the argument to have the default value "./path/to/data-file.csv" but then you disregard the argument and always initialize with the default value. Commented Apr 10, 2017 at 14:58

2 Answers 2

2

There are two significant problems:

  1. since you are using gcc 4.8.5, remember to use the flag -std=c++11 otherwise std::unique_ptr will not be available.

  2. end your class definition with a ;. The semi-colon is required in C/C++ when declaring types. Because you did not use a ;, you did not declare myClass as a type, and so it follows that the line static std::unique_ptr<myClass> classPtr; will produce an error since myClass is not a valid type.

Sign up to request clarification or add additional context in comments.

7 Comments

@VladislavMartin Your example does not illustrate how you are initializing your unique_ptr. It would be important to show it if that's what's causing you problems.
I cannot get the updated example to produce the error you described. You will need to supply a minimal reproducible example. Make a short self contained example that, when compiled and executed, produces the error you observe.
@VladislavMartin You have not. The example has a few errors but none that I found that could produce the error you described. Fixing them, I am able to compile the example without error. Take the example you provided, compile it by itself as a new project. If you can reproduce the error, indicate which line is causing it and which compiler you are using.
All the errors I'm getting are related to the unique_ptr not being initialized correctly. I am compiling with g++ (GCC) 4.8.5 (Read Hat 4.8.5-4) on a Linux machine. The error is NOT thrown in the class definition - it's thrown in a source file that included the class, on line 4 (I've added a comment in my question showing where).
Are you using a flag such as -std=c++11?
|
0

Your initialization of your data member should either be

data(std::vector<double>())

Or more simply

data()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.