I have a class called Thing that defines vector<string> stuff.

I have no problem declaring anything I want inside "class Thing" itself or functions from it, like "void Thing::bob" works fine. My problem is when I inherit this class. For some reason I get my program terminated.

pseudocode:

Class Thing
{
protected:
	vector<string> stuff;
private:
	constructers();
	~destructors();
};

Thing::constructors()
{
	stuff //do whatever I want with it without problems.
}

Class Foo : Public Thing
{
private:
	constructers();
	~destructors();
};

Foo::constructers()
{
	stuff //can't mess with it.
	cout << stuff.back(); //this would give me a runtime error.
}

In my "Thing::constructors()" above in my pseudocode I'm not doing anything fancy with vector<string> stuff. I'm just adding variables too it like "house" "farm" and your standard strings. They display with std::cout, but not when it's inherited too Foo.

Does this have to do with the behaviors of vectors or could there possibly be a memory leak with my program? Perhaps I’m just declaring my vector<string> stuff wrong.

How about you give us some real code that compiles and gives you the error. :icon_rolleyes: It's kind of hard to tell you how your code is wrong when you summarize it using pseudocode.

You can't inherit from the class with private constructors and/or destructors.
So it's doubtful you catch run-time error: you can't compile С++ code based on this pseudocode.
If std::vector is empty then v.back does not return consistent std::string.
Please, present more adequate info about your problem.

As request, I've posted some working code.

Basically I'm parsing a file for strings, using ';' as a delimter. I can do the parsing, but my problem is still as described in the first post.

NOTE: This code is still presudocode. The reason being is because my app, and the classes themselves are too full of other stuff to post. You'd have to wad through tons of junk to get to the actual problem I have. Which is why I created this code below, its the same problem, just condensed.

The thing is, my problem below compiles and my app dosn't leading me to belive it's a memmory error. Could someone give me a lead on were to look next, or am I going to have to debug this thing.

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

class Thing
{
protected:
      std::vector<std::string> stuff;
public:
       Thing();
       void Test();
};

Thing::Thing()
{
}

void Thing::Test()
{
	std::ifstream filestr( "file.txt" );

	//some things...  What were parsing the file for
	std::string line;
	std::string search;

	//search the file
	while(!filestr.eof())
	{
        std::getline(filestr, line);
        // sample data
        if( "stuff" == line.substr(0, line.find(' ')) )
        {
	        search = line.substr(6, line.size());

            std::stringstream stream;
            stream << line.substr(6, search.size());

            std::string temp;
            while(std::getline(stream,temp,';'))
            {
                stuff.push_back(temp);
                std::cout << "stuff: " << stuff.back() << '\n';
            }
        }
    }
    filestr.close();
}

class Foo : public Thing
{
public:
       Foo();      
};

Foo::Foo()
{
     Test();
     std::cout << "stuff2: " << stuff.back() << '\n';
}

int main()
{
    Foo foo;
 
    system("PAUSE");
    return 0;   
}

Before you run this, the app requires a text file called file.txt. So right click and create a text file in the same location as this app. On the first line past this:

apples;oranges;peaches;pares;

If your real situation is exactly like this test, then it's because your data is wrong. Assigning all of the items to the vector is based on the line beginning with "stuff" followed by a space. So you're trying to get the last element of an empty vector, which doesn't exist, so naturally you should expect it to bomb.

This article has been dead for over six months. Start a new discussion instead.