1

What's wrong with this:

struct FileListItem {
    string  sOriginalFn;
    time_t  ttTimeTaken;

    FileListItem(){}
    FileListItem(string _sOriginalFn, time_t _ttTimeTaken) :
        sOriginalFn (_sOriginalFn), ttTimeTaken (_ttTimeTaken)  { }
};

struct FileList : vector<FileListItem> {
    int iCurItm;
    FileList() : vector(), iCurItm(-1) {};

    void Add(string _sOriginalFn, time_t _ttTimeTaken) { 
        push_back(FileListItem(_sOriginalFn, _ttTimeTaken)); 
    }
}

I get a run-time "read access violation" the first time Add is called.

I then try:

struct FileList : vector<FileListItem> {
    int iCurItm;
    FileList() : vector(), iCurItm(-1) {};

    FileListItem Itm;                    // <--- new member
    void Add(string _sOriginalFn, time_t _ttTimeTaken) { 
        Itm(_sOriginalFn, _ttTimeTaken); // <--- E0980 pointing to "Itm"
        push_back(Itm); 
    }
}

and get a compile time error:

E0980 - call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type.

I must have forgotten or missed something since I stopped programming 30 years ago, when Borland C++ was IT...

9
  • 4
    It is usually considered bad practice to inherit from the standard library. It's better to have a struct containing a vector than to inherit from std::vector Commented Aug 6, 2018 at 7:28
  • Sorry, I was wrong, you can access push_back even with private inheritance from within you derived class. Note that there is a typo in FileList() : vector(), iCurItm(-1) {};, the semicolon is superfluous at the end. Commented Aug 6, 2018 at 7:30
  • VS2017 did not give any runtime error when Add was called. what is your compiler? Commented Aug 6, 2018 at 7:31
  • Using VS2017. I eliminated , iCurItm(-1). and eliminated the superfluous ; typo. And still - getting the same runtime error. miradham - can you show the full program you used to test it - maybe it's something about the way I first create or call my objects? Commented Aug 6, 2018 at 9:53
  • 1
    We need a full reproducible example of the code being run in order to understand where that error is coming from Commented Aug 6, 2018 at 10:59

1 Answer 1

3

In the line

Itm(_sOriginalFn, _ttTimeTaken);

You are not calling the constructor for your object. Instead, the compiler interprets this as a function call on the functor Itm. Therefore, it tries to find a valid operator() for the given arguments, to no-avail.

Do not use constructor member initializer list syntax elsewhere than... Constructor member initializer list. Instead, what you have to do here is to assign a new object to your member.

Then, you don't want, ever to inherit from std containers. Their destructor is not virtual so you are exposing yourself to a realm of suffering...

Use composition or type aliasing instead:

struct FileList {
    std::vector<FileListItem> flist;
    int curItem;

    FileList() : flist(), curItem(-1) {}

    void Add(string const& _sOriginalFn, time_t _ttTimeTaken) {
    // No need to construct the object before, let vector handle it
    flist.emplace_back(_sOriginalFn, _ttTimeTaken);
};

As for the read access violation, from the code I see, nothing explains it. Could it be some access related to curItem somewhere in your code that isn't displayed here?

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

5 Comments

this does not explain read access violation OP has faced first
Just note that this will copy from rvalue arguments.
@DanielLangr indeed. I didn't want to overcomplicate things with a perfect forwarding design
I eliminated iCurItm from the constructor, and also eliminated the entire constructor - runtime error is exactly the same. Any ideas? @miradham - can you show the full program you used to test it - maybe it's something about the way I first create or call my objects?
OK, Got it! @miradham's code ran OK, but the stubborn memory over-run continued as long as FileList.Add was (elsewhere in the code, as @Retiro suggested) invoked through a local variable defined: FileList &FL;. Once it changed to FileList *FL; - There was no problem. I must have done something absolutely TERRIBLE! though I wonder how, with thousands of compiler errors/warnings, this was not caught. Thanks everybody for help and advice!

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.