Cannot get my getters to work!?

Here is my code, not working =(

FILE: main.cpp:
1
2
3
4
5
6
7
8
9
#include "cscd305_w12_lab3.h"

int main()
{
	CArray integers1(7); // seven-element CArray
	CArray integers2; // 10-element CArray by default

	// print integers1 size and contents
cout << "Size of CArray integers1 is " << integers1.getSize() << endl;


FILE: cscd305_"w12_lab3.h"
1
2
3
4
5
6
7
#pragma once

#include "CArray.h";
#include <iostream>
#include <string>

using namespace std;


FILE: CArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "cscd305_w12_lab3.h"

CArray::CArray()
{this->m_array = new int[10];}

CArray::CArray(int size)
{this->m_array = new int[size];}

CArray::~CArray(void)
{delete[] this->m_array;}

int getSize(void)
{(sizeof this->m_array) / sizeof(int);}


FILE: CArray.h
1
2
3
4
5
6
7
8
9
10
11
12
class CArray
{
public:
	CArray();
	CArray(int size);
	~CArray();

	int getSize();

private:
	int *m_array;
};


Errors:

Error 2 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.h 2 error C2011: 'CArray' : 'class' type redefinition
Error 3 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 4 error C2027: use of undefined type 'CArray'
Error 7 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 9 error C2027: use of undefined type 'CArray'
Error 11 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 14 error C2027: use of undefined type 'CArray'
Error 4 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 4 error C2059: syntax error : ')'
Error 8 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 9 error C2062: type 'int' unexpected
Error 12 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 14 error C2062: type 'void' unexpected
Error 5 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 5 error C2143: syntax error : missing ';' before '{'
Error 9 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 10 error C2143: syntax error : missing ';' before '{'
Error 13 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 15 error C2143: syntax error : missing ';' before '{'
Error 6 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 5 error C2447: '{' : missing function header (old-style formal list?)
Error 10 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 10 error C2447: '{' : missing function header (old-style formal list?)
Error 14 c:\users\josh\documents\visual studio 2010\projects\cscd305\lab 3\carray.cpp 15 error C2447: '{' : missing function header (old-style formal list?)
m_array is a pointer. sizeof a_pointer gives you the size of a pointer, not the size of the allocated memory which the pointer points to. I don't know why so many people think sizeof works that way, but it doesn't. It's a poor way to get the size of an array. Instead, you should keep track of the allocated size in a separate variable.

Also, from the errors, it looks like you have some include problem (either you're not including the right file or you have a circular inclusion problem), but I don't see it in the code you posted. That all looks OK.

Do you have any other #include lines that you didn't post here?
Last edited on
Thats the entirety of the code... in fact, even without the sizeof, i simply tried to return 1, and it wouldnt work
OK I actually tried compiling it and found some problems:

1) get rid of the semicolon after #include "CArray.h";

2) in CArray.cpp, getSize needs to be CArray::getSize

3) CArray::getSize needs to actually return something

4) main needs to end in a closing brace


after those 4 changes the code compiles on my machine.


EDIT: removed one change which turned out to be a copy/paste error on my part
Last edited on
Thanks that CArray::getSize was what i needed most

also decided to use an int to track size of the array rather than size of this->m_array / size of int
since some say it doesnt work
Last edited on
Topic archived. No new replies allowed.