![]() |
|
#1
|
|||
|
|||
Error C2143: syntax error : missing ')' before '&'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": CPP / C++ / C Code:
CPP / C++ / C Code:
Code:
|
|||
|
#2
|
|||
|
|||
Re: Help!My first C++ class is messed up!How could you write so much stuff without testing along the way?
I would suggest you drop back to this and add / fix things until you're stumped and then ask: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Help!My first C++ class is messed up!As Howard suggests, perhaps you should take a peek at some reading about namespaces. Naturally, you can also tack the "using namespace std;" in the beginning of your files without knowing what it's for, but it's nice to know what actually it's for and what are the alternatives.
Why are your class data members base_row and base_mat references? Your constructor does a whole lot of work that would be better left to a dedicated member function. Besides, with your reference class members in place, there's no way you can construct your matrix that way. It makes sense if the constructor takes an argument of the type of your matrix. Or it makes sense if you have a member function addRow(vector<short>&), which adds a new row to the matrix. But for me it makes little sense to make the constructor start reading from files. Why are you static_cast'ing the return value from the vector's size()? What if the size is larger than what your int can hold? Probably unlikely, but one can wonder why return an int to begin with when the return value from size() is not an int. The declaration of sum() says it returns an unsigned long, but the definition says it returns an int. The base_row data member is useless. You only use it as a temporary variable to store a matrix row before writing it to base_mat. You could just use a real temporary variable in the method that reads the file. If you need the dimensions of the vector, then you can simply store them in separate data members, for example. This CPP / C++ / C Code:
CPP / C++ / C Code:
I decided to make a simple test program and ran it with gdb. Here are the results. it is an iterator, vint is a vector. Code:
Quote:
You just don't do it. You do what Howard said. |
|
#4
|
|||
|
|||
Re: Error C2143: syntax error : missing ')' before '&'Sorry for the late follow-up, but something I didn't notice the first time in your implementation.
CPP / C++ / C Code:
Anyway, here is one possible start for a matrix class based on what I said earlier. matrix.h: CPP / C++ / C Code:
matrix.cpp CPP / C++ / C Code:
main.cpp CPP / C++ / C Code:
Some things to note. There is no error checking of any kind. This also includes the forcing of the matrix dimensions. That is, there is nothing that prevent you from creating a 3x3 matrix and then adding a row with 50 columns. You could easily implement this by, for example, making m_rows and m_cols const. You would then have to pass the dimensions in a constructor since you cannot assign const members. CPP / C++ / C Code:
I didn't implement creating a matrix from a file since I don't think the Matrix class should do such a thing. For this you could define a global function or a class with a static method that reads the file and returns a vector<vector<short> > or a Matrix object. For example: CPP / C++ / C Code:
CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Returning a Struct to a separate class | usmsci | C++ Forum | 9 | 07-Feb-2007 09:34 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 20:23 |
| Box Class, need help again :( | TransformedBG | C++ Forum | 7 | 13-Nov-2006 16:11 |
| C++ class -- Please help | vnca_1 | C++ Forum | 3 | 14-Jun-2006 13:31 |
| a tester class and then some. | postage | Java Forum | 1 | 06-May-2006 16:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The