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 12-May-2009, 02:36
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

Anyone can fix the error for me?


Hi,
I am doing some exercise on file reading/writing and C++ vector operation. My code is as following:
CPP / C++ / C Code:
#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;
}

int 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;

    // Read the data back to a 2D vector.
	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 for condition of boundary elements.
	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());
	}

    // Output
	for (int row_num=1; row_num<3; row_num++)
	{
		for (int col_num=1; col_num<4; col_num++)
		{

			[color="Red"]mVect vec1(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]));[/color]
			vec1.print();
			cout<<endl;
		}
		cout<<endl;
	}
	in_file.close();
	return 0;
}

Specifically, what I want is to write a 2D matrix to a file named "test.bin" and then read the data back to a 2D C++ vector "vec_2d", after that, model the neighborhood of each elememts using a mathematic vector consists of some nearby elements and print them out respectively. In consideration for boundary elements, an extra padding operation is also carried out. However, there seems to be something wrong with the red colored code. In VS 2005,the error while compilation is listed as:
CPP / C++ / C Code:
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2057: expected constant expression
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(121) : 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(121) : error C2087: 'vec_2d' : missing subscript
e:\code zone\reference code\c++ snippets\advanced math vector class\advanced math vector class.cpp(125) : error C2228: left of '.print' must have class/struct/union


Anybody can tell me why does this happen and give me some guide about an effective solution?Thank u in advance.
nanchuangyeyu
  #2  
Old 12-May-2009, 09:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Anyone can fix the error for me?


[quote=nanchuangyeyu]
CPP / C++ / C Code:

    mVect vec1(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]));
}
This notation tells the compiler to create three temporary 2-D arrays of doubles and then use them as arguments to the constructor.

The error messages from the compiler may not seem to be particularly enlightening, since you don't recognize the problem, but that's often the case.


There are actually two errors here. Your compiler doesn't get past the first one.

1. Standard C++ does not support variable-length arrays.
2. The arguments to the constructor can be three doubles, not three arrays.


One possible solution: Use static_cast on the arguments:
CPP / C++ / C Code:
    mVect vec1(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]));

Another: Use old-style C casts:
CPP / C++ / C Code:
    mVect vec1((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]);


Another: Given that you have a constructor that takes three doubles, let the compiler figure out that is what needs and it will promote the arguments. In other words, it sees if the arguments can unambiguously be made compatible with any of the constructors that you have defined, and does the work of converting them.
CPP / C++ / C Code:
    mVect vec1(vec_2d[row_num][col_num - 1],
               vec_2d[row_num - 1][col_num - 1],
               vec_2d[row_num - 1][col_num]);

C++ purists would probably use the first one. I like it because there is absolutely no question as to the programmer's intent.

Many old-hand C programmers would use the second. C++ purists might point out that, although there is nothing "wrong" with this in this case, the gratuitous and habitual use of old-style casts can get you into trouble some day. Rather than give tiresome examples of how it can go wrong, I would suggest that cultivation good habits is important when people are learning.

Lazy and/or non-observant programmers might use the third one.

As for the third example, well I personally think that "letting the compiler figure it out" is not necessarily the best habit to get into either. The compiler won't get confused, but programmers trying to maintain and/or debug code like this can easily overlook problems that could arise.


Regards,

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

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

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

All times are GMT -6. The time now is 13:35.


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