Hi list,

Three functions use the following items vector. Where/how should i put the vector to make this possible ?
Any reference to related sources will be appreciated.

vector< vector<int> > items ( 6, vector<int> ( 6 ) );

P.S I tried to make it static but it gave error unfortunately .

Recommended Answers

Globals are EVIL. Period.
You have two ways:
1. declare the vector inside main() and pass a pointer (this is more C-way)

void foo(vector<vector<int> >* items){
};
void bar(vector<vector<int> >* items){
};
void foobar(vector<vector<int> >* items){
};

void main(){
	vector< vector<int> > items ( 6, vector<int> ( …
Jump to Post

All 4 Replies

just declare it in a class and mention your three functions in the class
make an object in main and access the data

Hi,
i am new in std::vector/cli::vector class. I dont know which one is more suitable for me. So i just want to see if it works in a single form application. Therefore its enought to make it global. Is this possible?
:?:

Globals are EVIL. Period.
You have two ways:
1. declare the vector inside main() and pass a pointer (this is more C-way)

void foo(vector<vector<int> >* items){
};
void bar(vector<vector<int> >* items){
};
void foobar(vector<vector<int> >* items){
};

void main(){
	vector< vector<int> > items ( 6, vector<int> ( 6 ) );
	foo(&items);
	bar(&items;
	foobar(&items);
}

2. make a simple class (C++-way)

class foobar{
public:
	vector< vector<int> > items ( 6, vector<int> ( 6 ) );
	void foo(vector<vector<int> >* items){
	};
	void bar(vector<vector<int> >* items){
	};
	void foobar(vector<vector<int> >* items){
	};
};

void main(){
	foobar foo;
	foo.items[0][0]=5;
	foo.foo();
	foo.bar();
	foo.foobar();
}

Thank you jaskij.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.