Hi,
I am a C++ newbie and had never written even a very simple C++ class before. Now I intend to do such a job. My objective is quite simple, a matrix class with the following features is just OK:
1. The object of such a class can be initialized by the data stored in a disk file and as a side information, the size of the desired matrix will be provided.
2. The summation of all the elements in the matrix should be obtained conveniently.
I have spend almost 2 days on this job and the following is my "achievement":
1. The definition of the class is included in a header file "matrix.h":

#include <fstream>
#include <vector>

typedef unsigned long ULONG;
class CMatrix
{
public:
	CMatrix(ifstream &in_file,int row,int col);    // Initialize(or construct) an matrix object with the data in a
	                                               // file, the width and the height of the matrix are both given.  
	ULONG sum();                                   // Get the summation of all the elements in the matrix.
	int s_row();                                   // Get the number of rows of the matrix, which may be trivial since 
	                                               // all the dimension information are already given as the argument of 
	                                               // constructor, but this may be more of "a member function",just a try.  
	int s_col();                                   // Get the number of the columns of the matrix.
private:
	vector<short>& base_row;      
	vector< vector<short> >& base_mat;   // Where all the elements of matrix are located.
};

CMatrix::CMatrix(ifstream &in_file, int row, int col)
{
	short val;
	while (in_file.read(reinterpret_cast<char*>(&val),sizeof(short)))
	{
		for (int rn=0; rn<row; rn++)
		{
			for (int cn=0; cn<col; cn++)
			{
				base_row.push_back(val);
			}
			base_mat.push_back(base_row);
			base_row.clear();
		}
	}
}

int CMatrix::sum()
{
	int _sum_1d = 0;
	int _sum_2d = 0;
	for (vector< vector<short> >::iterator it_2d = base_mat.begin(); it_2d<base_mat.size(); it_2d++)
	{
		for (vector<short>::iterator it_1d = base_row.begin(); it_1d<base_row.size(); it_1d++)
		{
			_sum_1d = _sum_1d + (*it_1d);  
		}
		_sum_2d = _sum_2d + _sum_1d;
	}
	return _sum_2d;
}

int CMatrix::s_row()
{
	return static_cast<int>(base_mat.size());
}

int CMatrix::s_col()
{
	return static_cast<int>(base_row.size());
}

2. The application using this class is included in a cpp file "matrix.cpp":

#include <iostream>
#include <string>
#include "matrix.h"

using namespace std;

int main(int argc, char** argv)
{
	if (argc!=2)
	{
		cout<<"Input error!"<<endl;
		cout<<"matrix_class.exe in_file"<<endl;
		exit(EXIT_FAILURE);
	}
	ifstream in_file(argv[1], ios::binary);     // argv[1] is the name of the file storing 9 numbers of short type and as a result the size is 18 BYTES.
	CMatrix myMat(in_file,3,3);
	cout<<"The sum of the matrix stored in file "<<argv[1]<<" is "<<myMat.sum()<<endl;
}

The necessary comments are already included in the code snippets.When I build the project in VS2005, as much as 40 errors are reported:

e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(8) : error C2143: syntax error : missing ')' before '&'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(8) : error C2143: syntax error : missing ';' before '&'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(8) : error C2460: 'CMatrix::ifstream' : uses 'CMatrix', which is being defined
        e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(6) : see declaration of 'CMatrix'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(8) : error C2062: type 'int' unexpected
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(16) : error C2143: syntax error : missing ';' before '<'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(16) : error C2238: unexpected token(s) preceding ';'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(17) : error C2143: syntax error : missing ';' before '<'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(17) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(17) : error C2238: unexpected token(s) preceding ';'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(20) : error C2065: 'in_file' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(20) : error C2597: illegal reference to non-static member 'CMatrix::ifstream'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(20) : error C2062: type 'int' unexpected
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(21) : error C2143: syntax error : missing ';' before '{'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(21) : error C2447: '{' : missing function header (old-style formal list?)
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(38) : error C2556: 'int CMatrix::sum(void)' : overloaded function differs only by return type from 'ULONG CMatrix::sum(void)'
        e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(10) : see declaration of 'CMatrix::sum'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(38) : error C2371: 'CMatrix::sum' : redefinition; different basic types
        e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(10) : see declaration of 'CMatrix::sum'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2065: 'vector' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2062: type 'short' unexpected
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2039: 'iterator' : is not a member of '`global namespace''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2065: 'it_2d' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2065: 'base_mat' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2228: left of '.size' must have class/struct/union
        type is ''unknown-type''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2143: syntax error : missing ';' before ')'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(41) : error C2143: syntax error : missing ';' before ')'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(42) : error C2143: syntax error : missing ';' before '{'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2062: type 'short' unexpected
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2039: 'iterator' : is not a member of '`global namespace''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2065: 'it_1d' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2065: 'base_row' : undeclared identifier
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2228: left of '.size' must have class/struct/union
        type is ''unknown-type''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2143: syntax error : missing ';' before ')'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(43) : error C2143: syntax error : missing ';' before ')'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(44) : error C2143: syntax error : missing ';' before '{'
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(54) : error C2228: left of '.size' must have class/struct/union
        type is ''unknown-type''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.h(59) : error C2228: left of '.size' must have class/struct/union
        type is ''unknown-type''
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.cpp(16) : error C2078: too many initializers
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.cpp(16) : error C2440: 'initializing' : cannot convert from 'int' to 'CMatrix'
        No constructor could take the source type, or constructor overload resolution was ambiguous
e:\code zone\practice code\my_first_class\matrix_class\matrix_class\matrix.cpp(17) : error C2264: 'CMatrix::sum' : error in function definition or declaration; function not called

I know it may seem lazy for me to paste all the code without localizing the potential errors. But by now that's really beyond my capability. Any one can tell me where things goes wrong and how to fix them? thank u in advance.

the library must be like this:
#include <iostream.h>
all of the library should have a .h or header

Comments
poor guy, you just got knocked the f--- out. Dragon hits hard, doesnt he? here's a bandage and a pat on the head.
wrong! current c++ headers do not have .h extension.

the library must be like this:
#include <iostream.h>
all of the library should have a .h or header

I normally don't give negative rep, but I couldn't help myself this time because your post is 100% wrong. Only an ancient compiler would use c++ header files with .h extension.

nanchuangyeyu: One reason for all those errors is because you need using namespace std; before including matrix.h or add it inside matrix.h. In that header file you could also just specify std:: before any STL templates, such as std::vector<std::vector<int>>

Comments
generous help,thanks

nanchuangyeyu: One reason for all those errors is because you need using namespace std; before including matrix.h or add it inside matrix.h. In that header file you could also just specify std:: before any STL templates, such as std::vector<std::vector<int>>

Thank u very much Ancient Dragon,
The number of errors has significantly declined to 7 after I moved the

using namespace std;

to the header file. However,the 7 errors left remain troublesome. Is there anything wrong with the class (including the member functions and the member variables) itself? thank u again for your help.

private:
	vector<short>& base_row;      
	vector< vector<short> >& base_mat;   // Where all the elements of matrix are located.

references have to be initialized in the class constructor. I suspect you do not want those to be references, so remove the & reference operator. for (vector<short>::iterator it_1d = base_row.begin(); it_1d<base_row.size(); it_1d++) That is the incorrect way to check for end-of-vector. Test the iterator against base_row.end(), not .size().


There are several other errors, but you should be able to correct those yourself.

This question has already been answered. Start a new discussion instead.