C++ newbie needs help splitting template class/2D array example into .h, .cpp,

RBS

New member
Joined
Oct 9, 2008
Messages
5
Reaction score
0
Points
1
and main() files? Hello all:

I am a C++ newbie. My goal is to use C++ to solve finite element/ finite difference problems. Contributors cja and Jammy very kindly helped me to understand an example I found of how to create a 2D array. Cja even provided a fix of the example (thanks cja for all your effort! This really switched on the lights for me).

What I would like to do now is figure out how to split the example up (example is below) into a header file, a .cpp file, and a main file--both to learn how to do this and because I am building on the example and things are getting complicated. I have tried to do this and keep getting error messages. The problems seem to be related to scope (e.g., an object is not declared in this scope).

I have tried to fix this by trying all sorts of combinations of Classname::, but nothing works. I am pretty sure that I have managed to get things right in the header and .cpp files (e.g., in the .cpp file, I have used prefixes such as: template<typename T> int Grid2D::Grid2D). The problem seems to be in the main() file.

I realize that I haven't provided a lot of specific information about the errors, and that is because I may be on the wrong track regarding where they are coming from. Also, little changes lead to quite different errors and it would be too much to include all of them here. What would be very helpful would be if someone could take a shot at splitting up the example below into a .h, a .cpp, and a main file... Thank you for all your help so far everyone!

#ifndef _GRID2D_H
#define _GRID2D_H

template <typename T>
class Grid2D {
public:
Grid2D() : xsize_(0), ysize_(0), grid_(0) { }
Grid2D(size_t row, size_t col)
: xsize_(row), ysize_(col), grid_(new T[ xsize_ * ysize_ ])
{ }
~Grid2D() { delete[ ] grid_; }

void Make(size_t row, size_t col) {
xsize_ = row;
ysize_ = col;
grid_ = new T[ xsize_ * ysize_ ];
}

void Resize(double row, double col) {
xsize_ = row;
ysize_ = col;
delete[ ] grid_;
grid_ = new T[ xsize_ * ysize_ ];
}
void Delete() {
xsize_ = 0;
ysize_ = 0;
delete[ ] grid_;
grid_ = 0;
}
size_t Row() const { return xsize_; }
size_t Col() const { return ysize_; }
T& operator()(size_t x, size_t y) {
return grid_[ y*xsize_ + x ];
}
const T& operator()(size_t x, size_t y) const {
return grid_[ y*xsize_ + x ];
}
private:
size_t xsize_, ysize_;
T* grid_;
};

#endif

Usage example:

#include <iostream>
#include <iomanip>
#include "grid2d.h"

using namespace std;

struct Complex {
double real;
double imag;
Complex(double r, double i) : real(r), imag(i) { }
Complex() : real(0.0), imag(0.0) { }
};

int main(int argc, char *argv[]) {
Grid2D<Complex> cMat1;
Grid2D<Complex> cMat2(2,3);

cout << "Dimensions of cMat1: " << cMat1.Row()
<< "x" << cMat1.Col() << endl;
cMat1.Make(1,2);

cout << "After cMat1.Make(1,2) call ..." << endl;
cout << "Dimensions of cMat1: " << cMat1.Row()
<< "x" << cMat1.Col() << endl;

cout << "Dimensions of cMat2: " << cMat2.Row()
<< "x" << cMat2.Col() << endl;

cMat1(0,0) = Complex(0.1,0.2);
cMat2(1,2) = Complex(1.1,2.2);

cout << cMat1(0,0).real << showpos
<< cMat1(0,0).imag << endl;
cout << noshowpos << cMat2(1,2).real
<< showpos << cMat2(1,2).imag << endl;

cMat2.Delete();

return 0;
}

#if 0

Program output:

Dimensions of cMat1: 0x0
After cMat1.Make(1,2) call ...
Dimensions of cMat1: 1x2
Dimensions of cMat2: 2x3
0.1+0.2
1.1+2.2

#endif
 
Back
Top