send an array to the consturor

Hi there!

i have this 2d double pointer array (?),called graph. I would like to read in a text file to another array lets call it cities, then send that array to my constructor, and then copy the content of cities to graph, the problem is that graph is string ** graph and cities is string cities[];

my second problem is that when I try to send that array cities to the constructor VS crashes.

constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Graph::Graph(string cities[], int n)
{
	this->nrOfCities=n;
	this->x=n;
	this->y=n;
	this->graph=new string *[n];
	for (int i = 0; i < x; i++) 
		this->graph[i] = new string[y];

	for(int i=0;i<this->x;i++)
	{
		//this->graph[i]=cities[i];
	}
}


read from file function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Graph::readFile()
{


	ifstream in("cities.txt");
	string city=" ";
	int nr=0;
	
	in>>nr;
	string cities[10];//can I somehow put nr inside the brackets instead of //10, or does it have to be a constant.
	if(!in.fail())
	{
		for(int i=0;i<=nr;i++)
		{
			getline(in,city);
			cities[i]=city;
			//cout<<cities[i]<<endl;
		}
	}
	else
		cout<<"ERROR!!!"<<endl;
	Graph(cities,nr);//this is where I try to send the array and nr of //cities to the constructor and where VS collapses.
	in.close();
}
can I somehow put nr inside the brackets instead of //10, or does it have to be a constant.
Use "new" like in the constructor.

In nr less than 10 ? If not, that might be the problem..
I don't really see what could be the problem. Does VS throw a runtime error before entering the constructor?

By the way, line 22 construct an object and does nothing with it. If your intention was to reconstruct "this", you can't. If you need another Graph, give it a name.
I think Ive somehow solved the read from file problem.
Do I really create a 2d array in my constructor? if so how do I assign the content of cities[] to the first row and column?
If its ok then it should be a 4X4 matrix.
It is a 2d array. Do read http://www.cplusplus.com/forum/articles/17108/ though. You might find it easier to use a normal array instead.

Now, if you have a 2d array and want to copy its first row form a 1d array, then
for(int i = 0; i < n; i++) array_2d[0][i] = array_1d[i];
Yeah I thought it would be something like that, the problem is that my 1d array is just a array and the 2d array is a string ** array2D, so I cant just copy the content from one to another.
Why not? As long as you allocate the memory you need, it's ok.
yeah but I've never allocated memory for my 1d array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Graph::Graph(string cities[], int n)
{
	this->nrOfCities=n;
	this->x=n;
	this->y=n;

	this->graph=new string *[x];
	for (int i = 0; i < x; ++i) 
		this->graph[i] =new string[y];
	for(int i=0;i<this->x;i++)
		for(int j=0;j<this->x;j++)
			this->graph[j]=NULL;
	for(int i=0;i<n;i++)
            graph[0][i]=cities[i];//this is what you mean?
	

}
You don't need to allocate memory for cities. It's a static array, it is on the stack, it is allocated automatically.
Your line 14 is good, but what are lines 10-12 supposed to do? They just throw away the memory you just allocated.. Remove them.
So you mean if I remove lines 10-12 then I've created an 2d array and copied the content of the cities array to the first row of my 2d array? If so how can I present the content of my 2d array?
would this work?
1
2
3
for(int i=0;i<3;++i)
     for(int j=0;j<3;++j)
	 cout<<this->graph[i][j]<<endl;
Yes.
Yes. Though you could change it to print
a b
c d
instead of
a
b
c
d
you have now.
what would I have to change?
But when I compile I get like 8-10 empty lines and thats it...
1
2
3
4
for(...;...;...){
   for(...;...;...) cout << ... << "  ";
   cout << '\n';
}


Do you call this in the constructor?
have a separate show function.
I can't say what is wrong with your code.
However, here's a working example of what you are doing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main(){
    std::string str[5] = {"prune", "is", "not", "a", "vegetable"};
    std::string** table = new std::string*[5];

    for(int i = 0; i < 5; i++) table[i] = new std::string[5];

    for(int i = 0; i < 5; i++) table[0][i] = str[i];

    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++) std::cout << table[j][i] << " ";
        std::cout << '\n';
    }
    std::cin.get();
    return 0;
}
Topic archived. No new replies allowed.