| | |
working with array of structs
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Solved Threads: 0
For my project I'm using an array of structs that I declared in the header file
now when I'm trying to initialize it in a constructor, like
I get an error:
left of '.visits' must have class/struct/union
Is that due to the map being a pointer? Anyone can point me in a right direction for solving this problem?
C++ Syntax (Toggle Plain Text)
struct City{ string name; int visits; }; City* map[200];
now when I'm trying to initialize it in a constructor, like
C++ Syntax (Toggle Plain Text)
for (int ii=0; ii<200; ii++) map[ii].visits = 0;
I get an error:
left of '.visits' must have class/struct/union
Is that due to the map being a pointer? Anyone can point me in a right direction for solving this problem?
When you are doing City* map[200] , map is an array of 200 pointers to the type City. So in order to access the members of the City struct using its pointers you need to use the -> operator.
Here are the two ways you can do it:
But, even if you do so your program won't work. Why ? Because you are not allocating memory to the pointers so acutally what they are poniting to is junk i.e. memory which doesn't belong to you. Consider allocating memory to the pointer before using it in your program.
Here are the two ways you can do it:
C++ Syntax (Toggle Plain Text)
*map[i].visits = 100 ; // OR map[i]->visits = 100 ;
But, even if you do so your program won't work. Why ? Because you are not allocating memory to the pointers so acutally what they are poniting to is junk i.e. memory which doesn't belong to you. Consider allocating memory to the pointer before using it in your program.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Do, to piggy back on the answer by Mr. ~s.o.s~ :mrgreen: you should probably just declare the map as
I also hope this line is not in your header file. It should be in your code file only.
City map[200];. That would fix your problem.I also hope this line is not in your header file. It should be in your code file only.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
Do, to piggy back on the answer by Mr. ~s.o.s~ :mrgreen: you should probably just declare the map asCity map[200];. That would fix your problem.
I also hope this line is not in your header file. It should be in your code file only.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Solved Threads: 0
~s.o.s~
The project will require me to dynamically allocate memory, not for this array, but for a link list I'll also need to have.
Here's what I've done for this array:
in the header file I created a pointer to the struct
and in the constructor I did the following:
How is this solution? I believe this is how I'd be alocating memory dynamically, if I needed to. Just have City[array_size_variable]. Is this correct? Thank you for your help.
The project will require me to dynamically allocate memory, not for this array, but for a link list I'll also need to have.
Here's what I've done for this array:
in the header file I created a pointer to the struct
C++ Syntax (Toggle Plain Text)
City* map;
and in the constructor I did the following:
C++ Syntax (Toggle Plain Text)
map = new City[200]; for (int ii=0; ii<200; ii++) map[ii].visits = 0;
How is this solution? I believe this is how I'd be alocating memory dynamically, if I needed to. Just have City[array_size_variable]. Is this correct? Thank you for your help.
One important thing in programming is to learn to evaluate your own code by writing it down and testing it with the help of the compiler.
Does the code which you pasted give you any errors ? No, then you are right atleast for the time being. Keep experimenting and you would learn two fold than what you are learning now.
And btw, yes your code is fine for the time being.
Does the code which you pasted give you any errors ? No, then you are right atleast for the time being. Keep experimenting and you would learn two fold than what you are learning now.
And btw, yes your code is fine for the time being.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
~s.o.s~
The project will require me to dynamically allocate memory, not for this array, but for a link list I'll also need to have.
Here's what I've done for this array:
in the header file I created a pointer to the struct
C++ Syntax (Toggle Plain Text)
City* map;
and in the constructor I did the following:
C++ Syntax (Toggle Plain Text)
map = new City[200]; for (int ii=0; ii<200; ii++) map[ii].visits = 0;
How is this solution? I believe this is how I'd be alocating memory dynamically, if I needed to. Just have City[array_size_variable]. Is this correct? Thank you for your help.
C++ Syntax (Toggle Plain Text)
City* map[200]; // declare an array of pointers ... for (int ii=0; ii<200; ii++) { map[ii] = new City; // load each pointer with a structure }
•
•
•
•
Originally Posted by s.o.s.
And btw, yes your code is fine for the time being.
Last edited by WaltP; Dec 22nd, 2006 at 4:53 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
No. You probably want:
C++ Syntax (Toggle Plain Text)
City* map[200]; // declare an array of pointers ... for (int ii=0; ii<200; ii++) { map[ii] = new City; // load each pointer with a structure }
It is? If so, I stand corrected. I've never seen it done that way.
Just an example of pointer being used to emulate an array...
An eg.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std ; struct City { string name ; int visit ; } ; int main( ) { cout << endl << "~s.o.s~'s method" << endl ; City* map = new City[3] ; for( int i = 0; i < 3; ++i ) { cout << "Enter name: " ; cin >> map[i].name ; getchar( ) ; } for( int i = 0; i < 3; ++i ) { cout << map[i].name << endl ; } cout << endl << "Mr. WaltP's method" << endl ; City* mapWalt[3] ; for( int i = 0; i < 3; ++i ) { mapWalt[i] = new City ; cout << "Enter name: " ; cin >> mapWalt[i]->name ; getchar( ) ; } for( int i = 0; i < 3; ++i ) { cout << mapWalt[i]->name << endl ; } }
My output:
•
•
•
•
~s.o.s~'s method
Enter name: abc
Enter name: efg
Enter name: hij
abc
efg
hij
Mr. WaltP's method
Enter name: abc
Enter name: efg
Enter name: hij
abc
efg
hij
Last edited by ~s.o.s~; Dec 22nd, 2006 at 5:17 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Help Me On These Questions...
- Next Thread: Doraemon's Song <this song will be out in internal spekaers>
Views: 3667 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






