-2

I want to share the same vector between derived classes and access the string as individuals. I get an error with unresolved externals. What is going wrong. Is there any other way of sharing a vector between classes without static methods.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class base
{
public:
  static vector<string> v1;

  void set(string val);

  vector<string> getval();
};

void base::set(string val)
{
   v1.push_back(val);
}

vector<string> base::getval()
{
  return v1;
}


class derived :public base
{
public:
  void vec();
};

void derived::vec()
{
  string b = "sfd";
  derived d;
  d.set(b);
}

class derive2:public base
{
public:
  void vec2();
};

void derive2::vec2()
{
  derive2 f;
  vector<string> a(100);
  a = f.getval();
}

int main()
{
  derived a;
  a.vec();
  derive2 b;
  b.vec2();
  a.vec();
  b.vec2();
  return 0;
}

How to get the vector in a derived class

Caninonos
  • 1,214
  • 7
  • 12
vaibhav
  • 41
  • 9

1 Answers1

1

You have to define the static class variable too. Add this line after the class definition:

 vector<string> base::v1;
Buddy
  • 10,874
  • 5
  • 41
  • 58