-2

I have a class that holds a vector, which also inherits another class:

class txtExt : public extention
{
private:
   string  openedFile_;
public:
   vector<string> txtVector; //the vector i want to call
};

I fill the vector in a method within a class:

class Manager : public extention
{
   // there is some other code here that I know does work
   // and it calls this function:
   void organizeExtention(string filename, string ext)
   {
      if(ext == "txt")
      {
         txtExt txtExt;
         txtExt.txtVector.pushback(filename);
      }
   }
}

and this is my main class where i attempt to call the vector:

int main()
{
   // some code here that does previous operations like getting the path
   // and filling the vector


   // I've tried many ways of trying to call the vector
   // here is an example of one:
   vector<txtExt*> testVector;
   for(int i = 0; i < testVector.size(); ++i)
   {
      cout << testVector[i] << endl;
   }
   return 0;
}

I have a few questions:

  1. Am I calling the vector wrong?
  2. Is my vector empty?
  3. Do I have to make my vector global, so other classes can see it?

Note: I've been able to print out the vector where I load the vector using a very simple for loop

Omonogo
  • 39
  • 1
  • 1
  • 5

2 Answers2

1

Well, as has been said you have a few errors in the code posted, and you maybe have some misunderstandings as well. But to answer the question asked, this

testVector[i]->txtVector

is the way to access the txtVector object that is inside each of your txtExt objects.

If that doesn't work for you then it's because one of the other errors/misunderstandings you have in your code.

john
  • 85,011
  • 4
  • 57
  • 81
0

To summarize:

reread the first chapters of a good C++ book ( The Definitive C++ Book Guide and List ), then try try to fix your program and deal with each error one at the time.

There are several errors in your code.

  • First of all, there's no operator << for printing entities of the type txtExt*.
  • Even object of type txtExt is not printable just like that.
  • In addition, the testVector you made is empty, so no .size() will be zero, and there's going to be no looping.
  • Are you really sure that you like to inherit both your classes from 'extension' ?
  • You can't call a vector, you can access it.
  • Having a data member (like the vector) public is not a good idea.
  • Calling a variable by the same name as a class is a very bad idea.

I have trouble guessing what your code should do. Here's a simple example of things you need to understand:

#include <iostream>
#include <vector>
#include <string>

class TxtExt 
{
public:
  std::vector<std::string> txtVector; 
};

int main(){
  TxtExt oneTxtExt;

  oneTxtExt.txtVector.push_back("hello");
  oneTxtExt.txtVector.push_back("world");
  for( auto &i : oneTxtExt.txtVector ){
    std::cout << i <<std::endl;
  }
}

The following code is correct, but has absolutely no effect. You could as well just write {}:

{
  TxtExt TxtExt;
  TxtExt.txtVector.pushback(filename);
}

You here create a new object, push back to it (btw it is called push_back), but then the object is destroyed at the end of the scope. Also, don't name you objects the same as the class, it becomes really confusing.

Community
  • 1
  • 1
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • Is there a correct way to point at the vector in my txtExt class, so I can print it out in my main class? – Omonogo Nov 03 '12 at 14:08
  • My Manager class is supposed to hold all my launcher objects, and my txtExt class is supposed to be a derived class from my base class(extention) – Omonogo Nov 03 '12 at 14:12
  • I guess there was no real reason to inherit from extension in Manager, it works fine without it at this point. and I meant to access the vector, not call it, my bad on that one. Kinda new to programming, don't know the exact terminology some times. – Omonogo Nov 03 '12 at 14:23
  • and also, I just used a ranged for, pretty much exactly as you have it there, and it didn't work. – Omonogo Nov 03 '12 at 14:24