Hi,
I am writing some code to model the neighborhood of an element in an 2D mathematic matrix(using 2D vector) ,e.g. to an element indexed by vec_2d[x][y], I want to present its neighborhood in form of a vector which is composed of vec_2d[x][y-1],vec_2d[x-1][y-1],vec_2d[x-1][y] and vec_2d[x-1][y+1]. And following my code is listed:

#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

class mVect
{
    private:
        // NO PRIVATE DATA
    public:
        double i, j, k;
        mVect();
        mVect(double,double);
        mVect(double,double,double);
        void print(); // NEEDS EXTRA LIBRARIES
};

// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
    i=0;
    j=0;
    k=0;
}

// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
    cout << "[" <<  i << "," << j << "," << k << "]";
}


// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
    i=a;
    j=b;
    k=0;
}

// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
    i=a;
    j=b;
    k=c;
}

void main()
{
	ofstream out_file;
	ifstream in_file;
    
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary | ios::app);

	short matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (int y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[y][x]),sizeof(short));
		}
	}
	out_file.close();
	cout<< "Matrix has been written to test.bin."<<endl;

	vector< vector<short> >vec_2d;
	vector<short>row;

 	in_file.open(out_name,ios::binary);
	short m;
	while(!in_file.eof())
	{
		for (int y=1; y<4; y++)
		{
			in_file.read(reinterpret_cast<char*>(&m),sizeof(short));
			if(in_file.eof())
				break;
			row.push_back(m);
			if (y == 3)
			{
				vec_2d.push_back(row);
				row.clear();
			}
		}
	}
    for (unsigned int i=0; i< vec_2d.size(); i++)
    {
		for (unsigned int j=0; j< vec_2d[i].size(); j++)
		{
			cout<<" "<<vec_2d[i][j];
		}
		cout<<endl;
    }
	// Pad vec_2d.

	vector< vector<short> >::iterator iter_2d;
    iter_2d = vec_2d.begin();
	vec_2d.insert(iter_2d,vec_2d.front());
	vec_2d.push_back(vec_2d.back());

	for (iter_2d=vec_2d.begin(); iter_2d<vec_2d.end();iter_2d++)
	{
		iter_2d->insert(iter_2d->begin(),iter_2d->front());
		iter_2d->push_back(iter_2d->back());
	}

	for (int row_num=1; row_num<3; row_num++)
	{
		for (int col_num=1; col_num<4; col_num++)
		{
			double pre_estimate = 0.25*(vec_2d[row_num][col_num-1] + vec_2d[row_num-1][col_num-1] + vec_2d[row_num-1][col_num] + vec_2d[row_num-1][col_num+1]);
			mVect vec1(double(vec_2d[row_num][col_num-1]-pre_estimate),
				       double(vec_2d[row_num-1][col_num-1]-pre_estimate),
					   double(vec_2d[row_num-1][col_num])-pre_estimate);			vec1.print();
			cout<<endl;
		}
		cout<<endl;
	}
	in_file.close();
}

and this code works well for a testing purpose,even not generating the desired result. But when I change the red colored code to

mVect intra_neighbor(double(vec_2d[row_num][col_num-1]),
                                                double(vec_2d[row_num-1][col_num-1]),
                                                double(vec_2d[row_num-1][col_num]));

errors occured while compilation:
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2466: cannot allocate an array of constant size 0
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(138) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(148) : error C2065: 'vec1' : undeclared identifier
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(148) : error C2228: left of '.print' must have class/struct/union
type is ''unknown-type''.

(138 is just the location of the newly changed code.)

Anybody can tell me why and give me some clue about how to efficiently model the neighborhood using an object of the given mVect class? Thank u in advance.

nanchuangyeyu

How about using C++ style type-casting instead of C-style:

mVect intra_neighbor(static_cast<double>(vec_2d[row_num][col_num-1]),
                static_cast<double>(vec_2d[row_num-1][col_num-1]),
                static_cast<double>(vec_2d[row_num-1][col_num]));

Or better: Modify your class to take ints as parameters instead of doubles. You're not doing anything with numbers behind the comma right?

Thank you for your help niek_e, it works well now.But why in the first case did the code works well with C-style type casting while the second encounter a failure?

As the compiler told you: It's expecting a const value. So this:

mVect intra_neighbor((const double)(vec_2d[row_num][col_num-1]),
                                                (const double)(vec_2d[row_num-1][col_num-1]),
                                                (const double)(vec_2d[row_num-1][col_num]));

could also work. (untested)

This article has been dead for over six months. Start a new discussion instead.