GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Oct-2008, 17:27
shumi shumi is offline
New Member
 
Join Date: Oct 2008
Posts: 1
shumi is on a distinguished road

Vector array of object pointers


I am facing a problem while executing the below c++ code. Could you please tell me where I have gone wrong. I am very new to c++.

CPP / C++ / C Code:
#include "stdafx.h"
#include "vector"
#include "iostream"

using namespace std;

class abc
{
public:
	int a;
	int *b;
	int c;
public:
	abc();
	~abc();
};
abc::abc()
{
	a=1;
	b=NULL;
	c=3;
}
abc::~abc()
{
	if(b != NULL)
		b =NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<abc *> Arr1;
	Arr1.resize(10);

	for(int i=0; i<10; i++)
	{
		abc obj;
		obj.a = i;
		obj.b = (int *)malloc(10*sizeof(int));
		for(int j=0; j<10; j++)
			obj.b[j] = i;
		obj.c = i+1;
		
		Arr1[i] = &obj;
	}

	for(int l=0; l<10; l++)
	{
		cout << Arr1[l]->a << "\t(";
		for(int m=0; m<10; m++)
               {
                        // getting an error at this place
			cout << (Arr1[l]->b)[m] << ", ";  
               }
		cout << ")\t" << Arr1[l]->c << endl;
	}
	cin.get();
	return 0;
}

Could any please correct the error I am facing above.
Last edited by admin : 02-Oct-2008 at 00:29. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 01-Oct-2008, 18:25
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Vector array of object pointers


Quote:
Originally Posted by shumi
i am facing a problem while executing the below c++ code.
You have many misperceptions about C++:
  • It is unclear whether this is meant to be a console application or an MFC application. This subforum is for generic platform agnostic programming. Microsoft-specific questions should go in the Microsoft forum.
  • A standard C++ application requires a main() function. _tmain() is MFC specific.
  • Standard headers require angle brackets. Header files expected to be found in the current directory are denoted by double quotes. The following:
    CPP / C++ / C Code:
    #include "vector"
    #include "iostream"
    ...should be replaced by:
    CPP / C++ / C Code:
    #include <vector>
    #include <iostream>
  • The class definition for abc has two public sections. I can only assume that you were experimenting with relaxing member access.
  • The principal problem in your code is that the objects created within the scope of your for-loop (What you are naming obj...) are destroyed at the end of the loop regardless of whether you save a pointer. The question you need to answer to yourself is why create an array of pointers? Why not create an array of objects?
  • CPP / C++ / C Code:
    (Arr1[l]->b)[m]

    ...is incorrect syntax. If you ultimately want to use an array, you will want to use the following instead:
    CPP / C++ / C Code:
    Arr1[l]->b[m]
Lastly, when posting code, please place a [cpp] code tag before & [/cpp] aftewards to both preserve formatting & color-code keywords & identifiers.
  #3  
Old 03-Oct-2008, 03:18
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Vector array of object pointers


Use smart pointer instead.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using pointers to object(s) from another object pfanning C++ Forum 7 20-Dec-2007 16:00
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 07:46
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.