Click here to Skip to main content
15,888,301 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: problem in C++ programming Pin
CPallini3-Apr-09 4:52
mveCPallini3-Apr-09 4:52 
QuestionRe: problem in C++ programming Pin
David Crow3-Apr-09 6:35
David Crow3-Apr-09 6:35 
AnswerRe: problem in C++ programming Pin
Thomas DeSalvo3-Apr-09 14:15
Thomas DeSalvo3-Apr-09 14:15 
GeneralRe: problem in C++ programming Pin
myprojectme3-Apr-09 20:59
myprojectme3-Apr-09 20:59 
QuestionMSADO15.tlh file not available in ..c:\documents and settings\user\local settings\temp\msado15.tlh Pin
pandit843-Apr-09 4:05
pandit843-Apr-09 4:05 
Questiondynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 2:36
Sauce!3-Apr-09 2:36 
AnswerRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson3-Apr-09 2:58
professionalStuart Dootson3-Apr-09 2:58 
GeneralRe: dynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 4:13
Sauce!3-Apr-09 4:13 
Stuart Dootson wrote:

I think you'll find that's the way C/C++ arrays work - they are just pointers really. Your code works fine - when I run this code, it prints out addresses that are 40 bytes apart, which is what I'd expect for rows of 10 ints.

Array2D<int> arr(10, 10);

int main(int, char**)
{
   for (int i=0;i<10;++i)
      std::cout << (int)arr[i] << std::endl;
   return 0;
}</int>


We seem to have a misunderstanding here.

It's a multi-dimensional array, remember? Here's
main.cpp<br />
<br />
<pre>#include <iostream><br />
#include "Array2D.h"<br />
<br />
typedef unsigned int UINT;<br />
using namespace std;<br />
<br />
int main()<br />
{<br />
	UINT width = 3, height = 5;<br />
	Array2D<int> *theArray;<br />
	theArray = new Array2D<int>(width, height);<br />
<br />
<br />
        //Get input to fill theArray<br />
	for(UINT i = 0; i < width; i++)<br />
	{<br />
		for(UINT j = 0; j < height; j++)<br />
		{<br />
			cout << " [" << i << "][" << j << "]:";<br />
			int temp = 0;<br />
			cin >> temp;			<br />
			*theArray[i][j] = temp;<br />
		}<br />
		cout << "\n";<br />
	}<br />
<br />
        //Print the contents of theArray<br />
	for(UINT i = 0; i < width; i++)<br />
	{<br />
		for(UINT j = 0; j < height; j++)<br />
		{<br />
			int temp = 0;<br />
			temp = *theArray[i][j];<br />
			cout << temp << " ";<br />
		}<br />
		cout << "\n";<br />
	}<br />
	<br />
	return 0;<br />
}</pre><br />
You'll find that we are able to assign <code>temp
to *theArray[i][j] fine for values [0][0] through [0][3], however as soon as we attempt to access [1][0] then an access violation occurs. This happens for the reasons I stated in my initial post.

Stuart Dootson wrote:
Also - your template class won't really be usable, as your client code won't be able to instantiate the size constructor or destructor - you don't define template class methods in .cpp files!

Ah, I was only getting link errors for the Array2D(Width, Height) constructor and operator[] so I figured those were the only ones that needed to be moved to the .h file. Can anyone link me to a good article that explains why template functions must be defined in the header?

modified on Friday, April 3, 2009 10:24 AM

GeneralRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson3-Apr-09 6:48
professionalStuart Dootson3-Apr-09 6:48 
GeneralRe: dynamic 2D array template for non-built-in types. [modified] Pin
Sauce!3-Apr-09 20:19
Sauce!3-Apr-09 20:19 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Stuart Dootson4-Apr-09 2:43
professionalStuart Dootson4-Apr-09 2:43 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Sauce!4-Apr-09 5:32
Sauce!4-Apr-09 5:32 
AnswerRe: dynamic 2D array template for non-built-in types. Pin
Maximilien3-Apr-09 3:00
Maximilien3-Apr-09 3:00 
AnswerRe: dynamic 2D array template for non-built-in types. Pin
Iain Clarke, Warrior Programmer4-Apr-09 0:06
Iain Clarke, Warrior Programmer4-Apr-09 0:06 
GeneralRe: dynamic 2D array template for non-built-in types. Pin
Sauce!4-Apr-09 0:19
Sauce!4-Apr-09 0:19 
QuestionHow do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 2:13
blokkies3-Apr-09 2:13 
AnswerRe: How do i create an Executable (.exe) to debug my project? Pin
Rajesh R Subramanian3-Apr-09 2:29
professionalRajesh R Subramanian3-Apr-09 2:29 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 2:43
blokkies3-Apr-09 2:43 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
Rajesh R Subramanian3-Apr-09 3:20
professionalRajesh R Subramanian3-Apr-09 3:20 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 3:40
blokkies3-Apr-09 3:40 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
Cedric Moonen3-Apr-09 3:52
Cedric Moonen3-Apr-09 3:52 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 4:03
blokkies3-Apr-09 4:03 
QuestionRe: How do i create an Executable (.exe) to debug my project? Pin
David Crow3-Apr-09 4:13
David Crow3-Apr-09 4:13 
AnswerRe: How do i create an Executable (.exe) to debug my project? Pin
blokkies3-Apr-09 4:20
blokkies3-Apr-09 4:20 
GeneralRe: How do i create an Executable (.exe) to debug my project? Pin
David Crow3-Apr-09 4:23
David Crow3-Apr-09 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.