0
const struct config{
    private:
        std::string file;
        config() : file(getConfig()) {}
    public:
        std::string failsafe = file.substr(file.find("failsafe") + 12);
        failsafe.resize(failsafe.find("\""));
        size_t ran, price, expiry;
    } *ini;

Line 7 - Error: this declaration has no storage class or type specifier. I'm puzzled. Why can't I access failsafe?

2
  • 1
    You can't call functions like substr or resize inside a structure declaration. Use your constructor to do this. Commented Apr 13, 2014 at 2:17
  • 1
    You can't run code inside a struct like that. Everything except for declaring your fields has to be inside a function. Commented Apr 13, 2014 at 2:17

2 Answers 2

1

There are several problems with your code. I put your code, plus added some dummy code (ie: getConfig) into MSVC2010 to test and fix it.

  • In nearly all cases, Constructors ( config() ) have to be public. After all, how else will it be accessed to initialize the object. I moved the constructor to the Public area.
  • The code (find(), substr()) must be inside a function. Either main(), the constructor of the object, or some function/method. I moved your code into the constructor.
  • I did not add anything to handle errors (such as not finding the substrings you are looking for), but you may consider adding some code to handle those situations.

const struct config{
private:
    std::string file;
public:
    config() : file(getConfig()) {
        failsafe = file.substr(file.find("failsafe") + 12);
        failsafe.resize(failsafe.find("\""));
    }
    std::string failsafe;
    size_t ran, price, expiry;
} *ini;
Sign up to request clarification or add additional context in comments.

Comments

1

The reason Constructors are there is for initializing members, which you try to attempt when you are declaring them.

const struct config{
    private:
        std::string file;
        config() : file(getConfig()) {
            failsafe = file.substr(file.find("failsafe") + 12);
            failsafe.resize(failsafe.find("\""));
        }
    public:
        std::string failsafe; 
        size_t ran, price, expiry;
    } *ini;

1 Comment

The file.substr() line may not do what you intend if failsafe is not found in the string, it'd be better to store the result of find and check it was found before doing the substr.

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.