Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++, array of pointers to objects

Posted on 2013-01-22
1
Medium Priority
?
246 Views
Last Modified: 2013-01-22
Hello experts, please have a look at the code below:

#include <iostream>
using namespace std;

class Dog{
public:
	int x;

};

int main(){
	Dog * matrix[12];
	int * matrixInt[12];

	*matrixInt[0] = 12; //works fine
	*matrix[0]->x = 12; //does not work

	return 0;
}

Open in new window


How can I make it work?
Thank you

panJames
0
Comment
Question by:panJames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 31

Accepted Solution

by:
Zoppo earned 1500 total points
ID: 38804963
Hi panJames,

you're declaring array to pointers, but the pointers are uninitialized. You need to allocate memory for them, i.e.:
Dog* matrix[12];
for ( int i = 0; i < 12; i++ )
{
 matrix[i] = new Dog;
}
...
// need to clean up when you don't need it anymore.
for ( int i = 0; i < 12; i++ )
{
 delete matrix[i];
}

Open in new window

(do it similar for the matrixInt)

Hope this helps,

ZOPPO
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question