Another question on classes

This is a discussion on Another question on classes within the C++ Programming forums, part of the General Programming Boards category; Hi again, I have a question about classes again. The book that I have been going through has finally expounded ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Another question on classes

    Hi again, I have a question about classes again. The book that I have been going through has finally expounded a bit about classes and we've been discussing constructors/deconstructors, etc The following is an exercise from the book. I have to implement the class Matrix (most of which was provided for me) and then create member functions to use with this class. When I am all said and done, I should be able to initialize an object to represent a matrix with r number of rows and c number of columns and the initial value of the elements should be v. I also know that in my matrix.cpp I have not created member functions for RowSize, ColumnSize and NumberRows as of yet. I wasn't completely clear on what to include in those, so I have not completed that portion yet. The book says that RowSize(int i) should return the size of row i, ColumnSize(int i) should return the size of column i and NumberRows(int i) should return the number of rows in the matrix.

    I will paste my code below and am open to any suggestions or ideas. I will also paste the errors that I am getting (bear with me though, since there are many of them). On a sidenote: I have dealt with other issues in this book where (in VC ++ 6.0) it has required that a .h file is in the "External Dependcies" under the Fileview project tab. Perhaps that is part of my issue here as well? (Guessing).

    Here's the code:

    ex_1155.cpp:

    Code:
    #include <iostream>
    #include <string>
    
    #include "matrix.h"
    #include "intlist.h"
    
    using namespace std;
    
    int main (){
    	Matrix A(3,4,1);
    	cout << A ;
    	return 0;
    }
    matrix.h (intlist is another part of this which interfaces with the code):

    Code:
    #ifndef MATRIX_H
    #define MATRIX_H
    
    #include <iostream>
    #include <string>
    
    #include "intlist.h"
    
    
    class Matrix {
    	public:
    		// default constructor
    		Matrix(int r=10, int c=10, int v=0);
    		// copy constructor
    		Matrix(const Matrix &M);
    		// destructor
    		~Matrix();
    		// assignment operator
    		Matrix& operator=(const Matrix &M);
    		// inspector for row of constant matrix
    		const IntList& operator[](int i) const;
    		// inspector for row of non-constant matrix
    		IntList& operator[](int i);
    		// inspector for a size of a given row
    		int RowSize(int i) const;
    		// inspector for a size of a given column
    		int ColumnSize(int i) const;
    		// inspector for a number of row
    		int NumberRows(int i) const;
    	private:
    		// data members
    		int NumRows;	// Row size of matrix
    		IntList *Values;	// pointer to rows
    };
    
    #endif
    matrix.cpp:

    Code:
    #include <assert.h>
    
    #include "matrix.h"
    
    // Constructor
    Matrix::Matrix(int r, int c, int v){
    	assert (r>0);
        NumRows=r;
    	Values = new int [r];
    	assert(Values);
    }
    
    // Copy constructor
    Matrix::Matrix(const Matrix &M){
    	int NumRows=M.NumberRows;
    	Values=new int [NumberRows];
    	assert(Values);
    }
    
    // Destructor
    Matrix::~Matrix(){
    	delete [] Values;
    }
    
    // Assignment operator
    Matrix::operator =(const Matrix &M){
    	int NumRows=M.NumberRows;
    	Values=new int [NumberRows];
    	assert(Values);
    	return Values;
    }
    
    // Inspector for row of constant matrix
    Matrix::operator[](int i) const{
    	assert((i>=0) && (i < RowSize()));
    	return Values[i];
    }
    
    // Inspector for row of non-constant matrix
    Matrix::operator[](int i) {
    	assert((i>=0) && (i < RowSize()));
    	return Values[i];
    }
    And here are the error messages:
    Compiling...
    ex_1155.cpp
    c:\program files\microsoft visual studio\myprojects\ex_1155\intlist.h(10) : error C2011: 'IntList' : 'class' type redefinition
    c:\program files\microsoft visual studio\myprojects\ex_1155\ex_1155.cpp(12) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrix' (or there is no acceptable conversion)
    matrix.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(9) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(15) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : warning C4551: function call missing argument list
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(26) : error C2556: 'int __thiscall Matrix:perator =(const class Matrix &)' : overloaded function differs only by return type from 'class Matrix &__thiscall Matrix:perator =(c
    onst class Matrix &)'
    c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(19) : see declaration of '='
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(26) : error C2040: '=' : 'int (const class Matrix &)' differs in levels of indirection from 'class Matrix &(const class Matrix &)'
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(27) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : warning C4551: function call missing argument list
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(34) : error C2556: 'int __thiscall Matrix:perator [](int) const' : overloaded function differs only by return type from 'const class IntList &__thiscall Matrix:perator [](int
    ) const'
    c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(21) : see declaration of '[]'
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(34) : error C2040: '[]' : 'int (int) const' differs in levels of indirection from 'const class IntList &(int) const'
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(35) : error C2660: 'RowSize' : function does not take 0 parameters
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(40) : error C2556: 'int __thiscall Matrix:perator [](int)' : overloaded function differs only by return type from 'class IntList &__thiscall Matrix:perator [](int)'
    c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(23) : see declaration of '[]'
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(40) : error C2040: '[]' : 'int (int)' differs in levels of indirection from 'class IntList &(int)'
    C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(41) : error C2660: 'RowSize' : function does not take 0 parameters
    Error executing cl.exe.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    disable smilies when your posting code that can turn into smilies... for example:

    Matrix:perator[](int i) {

    should be:

    Matrix::operator[](int i) {





    you need to put "using namespace std;" above the matrix class, because you #included it above that... i would just include it under that in the main()... that might fix at least the second error, and i'm guessing it would clear up a few more along with it...
    Last edited by major_small; 05-19-2003 at 08:36 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well, I've been busy with school lately, so I sorta left off in c++ at overloading operators, but I see at least one problem, and that is in ex_1155 where you do cout<< A; I believe you would have to do something like cout<< A.Rows; (assuming you had a function to return the rows) or maybe u can assign an operator that way (i dont think you attempted that?)


    Well, thats a lot of errors, so maybe you should go back and get rid of some stuff and see what works and what doesn't. Or use a debugger (which I know nothing about).
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well since you got the errors, i'm assuming you have some kind of debugging already... just take care of the ones on the top of the list first and ignore all the ones under it... usually when you fix the top error it takes care of a few of the ones under it...


    /edit: I just reread my last post.. let me clear it up:

    instead of:
    Code:
    #include <iostream>
    #include <string>
    
    #include "matrix.h"
    #include "intlist.h"
    
    using namespace std;
    
    int main (){
    	Matrix A(3,4,1);
    	cout << A ;
    	return 0;
    }
    use:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include "matrix.h"
    #include "intlist.h"
    
    int main (){
    	Matrix A(3,4,1);
    	cout << A ;
    	return 0;
    }
    Last edited by major_small; 05-19-2003 at 08:42 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How to use VC++ 6.0:
    The first thing you need to do before you start writing your program is to start a new project. You should close all workspaces before doing that by clicking on File/Close Workspace. Then click on File/New. The projects tab should be selected and you should make sure the radio button for "Create new workspace" is checked. For a console program, select Win32 Console Application, fill in a project name, then click OK. Then select Empty Project and click Finish. Then click on OK.

    Now you need to create a file for your project. Click on File/New and the Files tab should be selected. Click on C/C++ Header File or C++ Source File depending on which you want to create. Make sure the "Add to project" check box is checked. Fill in the File Name(with no extension--it will be added for you) and click on OK. At the top left of the Visual C++ window, in the blue border, it will show the project name and then Microsoft Visual C++. The file you created has its name in the blue border at the top of its window.

    Create as many files as you need for your project. The way you link the files in your project is with #include statements at the top of the various files. If you don't know how to #include things correctly for a multi-file project, I suggest you get a beginning C++ book, or study some online tutorials. Otherwise, you are set to compile and then execute your program.

    If your current project is completely messed up, I would start a new project with new files and copy and paste the code from your current files into the new files.

    The hierarchy goes like this: workspace/project/files. A project is simply a program of some kind made up of various files, and a project workspace is a folder in which all the information relating to a project is stored. When you create a project, a project workspace(a folder) is created automatically, and Visual C++ will maintain all the source code and other files in the project workspace folder. When you want to stop working on a program, you click on File/Close Workspace, and when you want to start working on a previous project, you click on File/Open Workspace or File/Recent Workspaces.

    The only way you can permanently delete files and/or workspaces is to navigate to the folder on your computer and delete one of the files in the folder or the whole folder.

    How to write a program:
    Write one function, and test it with some code in main() to make sure it works. Debug it until you get the function to work. Move onto the next function. You don't write a buch of functions and then try to compile the program with the result being 50 errors that are hard to track down, especially with a complex program.

    Tips on your program:

    A two dimensional array has two dimensions: rows and columns, so when you say your are supposed to write functions that return row size , column size, and number of rows, it doesn't make sense. Anyway, your constructor requires the rows and columns to create the array, so if you store those in data members, then RowSize() and ColumnSize() are typically called "get functions" and all they do is return the data member:

    RowSize()
    {
    return rows;
    }

    ColumnSize()
    {
    return columns;
    }

    What book are you using? It probably belongs in the trash with 90% of all C++ books.
    Last edited by 7stud; 05-19-2003 at 08:58 PM.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    "What book are you using? It probably belongs in the trash with 90% of all C++ books."

    The book is called C++ Program Design by Cohoon and Davidson. This book is a required text for the class that I am taking and my instructor would be the first to admit that it is one of the worst C++ programming books he's ever seen. (Why it was chosen is beyond me).

    Perhaps I should take it from the top again and make sure that all my ducks are in a row here. I did take this pretty carefully at first, but the thing of it is is that the only part of it I was given was the part that is my matrix.h code basically. The main code and the .cpp file were things that I created and I really can't test one part without the other two (or can I?). So, I basically had to try and get as much of it complete as I could in order to test the validity of what I was doing.. unless there is a better way that it can be done. I by no means profess to know all the answers, I defer to the experts

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    instead of
    Code:
    #include "matrix.h"
    #include "intlist.h"
    
    using namespace std;
    call them in this order:
    Code:
    using namespace std;
    #include "intlist.h"
    #include "matrix.h"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Here's the exact verbage of what it is asking for:

    "Implement a class Matrix that has the following class interface (it proceeds to show basically everything that I wrote in the matrix.h in my original post). The Matrix member functions have the following specifications:

    Matrix(int r=10, int c=10, int v=0): intializes the object to represent a matrix with r rows and c columns. The initial value of the elements is v.

    Matrix(const Matrix &M): initalizes object to be a deep copy of M

    ~Matrix(): returns the dynamic storage to which Values points.

    operator=(const Matrix &M): resets object to be a deep copy of M

    operator[](int i) const: reference returns row i of matrix in constant form

    operator[](int i): reference returns row i of matrix in non-constant form

    RowSize(int i) const: returns size of row i
    ColumnSize(int i) const: returns size of column i
    NumberRows(int i) const: returns the number of rows in the matrix

    "

    Hopefully this helps to explain what they expect me to accomplish...

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Code:
    using namespace std;
    #include "intlist.h"
    #include "matrix.h"
    changing the order that these were in did not seem have a positive or negative effect on things unfortunately. I did some re-working of things on my own and I have got it narrowed down to 12 error messages now. I just think that I am not "linking" things together correctly with VC++ 6.0. Maybe if someone can tell me the right way to link the class .h file and the class .cpp file together with the main source code in VC ++ 6.0, that might help me to solve some of my issues as well. The book that we're using doesn't speak to any one compiler specifically, so it's terribly mediocre in explaining things in general.

    Thanks!

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "So, I basically had to try and get as much of it complete as I could in order to test the validity of what I was doing.. unless there is a better way that it can be done."

    What prevents you from commenting out everything except what you're working on?

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    "What prevents you from commenting out everything except what you're working on?"

    Good point. I suppose that I should work it that way. I need to work on trying to divide the program up in seperate smaller programs and tackle one issue at a time I guess is the bottom line.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Maybe if someone can tell me the right way to link the class .h file and the class .cpp file together with the main source code in VC ++ 6.0, that might help me to solve some of my issues as well."

    my_program.cpp
    --------------------
    #include "my_class.h"


    my_class.h
    --------------



    my_class.cpp
    ------------------
    #include "my_class.h"



    Keep in mind that each of those files has to include any additional header files it needs, so if you use the name "cout" in my_class.cpp, then you also need this line:

    #include <iostream>

    using std::cout;
    [or using namespace std;]
    [or don't include a using declaration and include the namespace name "std" before cout--->std::cout<<"Hello world";}
    Last edited by 7stud; 05-19-2003 at 09:13 PM.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I am still struggling along with this. I think I am starting to understand classes better, but this one that I have to do is throwing me for a loop because it using another class that's defined in the book called IntList. If it were just the Matrix class, I'd probably understand better. In any case, here's what the book provides for the class interface:

    Code:
    class Matrix {
    	public:
    		// default constructor
    		Matrix(int r=10, int c=10, int v=0);
    		// copy constructor
    		Matrix(const Matrix &M);
    		// destructor
    		~Matrix();
    		// assignment operator
    		Matrix& operator=(const Matrix &M);
    		// inspector for row of constant matrix
    		const IntList& operator[](int i) const;
    		// inspector for row of non-constant matrix
    		IntList& operator[](int i);
    		// inspector for a size of a given row
    		int RowSize(int i) const;
    		// inspector for a size of a given column
    		int ColumnSize(int i) const;
    		// inspector for a number of row
    		int NumberRows(int i) const;
    	private:
    		// data members
    		int NumRows;	// Row size of matrix
    		IntList *Values;	// pointer to rows
    };
    The code above has been put into a matrix.h file along with the following at the beginning of the matrix.h file (I added these parts myself):

    Code:
    #ifndef MATRIX_H
    #define MATRIX_H
    
    #include <iostream>
    #include <string>
    
    #include "intlist.h"
    and an #endif at the end of the class interface. Based on this
    I have done the following so far with the matrix.cpp file:

    Code:
    #include <assert.h>
    #include <string>
    #include "matrix.h"
    
    using namespace std;
    
    // Constructor
    Matrix::Matrix(int r, int c, int v){
    	
    }
    
    // Copy constructor
    Matrix::Matrix(const Matrix &M){
    	Values=M.RowSize;
    	M[i]=Values[i];
    
    } 
    
    // Destructor
    Matrix::~Matrix(){
    	delete [] Values;
    }
    
    // Assignment operator
    Matrix& Matrix ::operator=(const Matrix &M){
    	return *this;	 
    }
    
    // Inspector for row of constant matrix
    const IntList& Matrix ::operator [](int i) const{
       	return 0;
    }
    
    // Inspector for row of non-constant matrix
    class Matrix IntList&::operator[](int i) {
    	  Values=new int 
    }
    
    int Matrix:: RowSize(int i) const { 
    	return RowSize(i);
    }
    
    int Matrix:: ColumnSize(int i) const{
    	return ColumnSize(i);
    }
    I have also made sure to include both the intlist.h and matrix.h in the header files folder in VC++ 6.0. The matrix.cpp is in the source code section. In my main code (which I can post but is pretty generic and just for testing), I simply am attempting to create an instance of the Matrix class. I am getting a variety of error messages at this point that I can post if someone would like to see them. I've gone from well over 100 errors to less then 10, so I guess that's progress. I have tried commenting out sections of both the matrix.cpp and the corresponding section of matrix.h and it seems like my constructor and destructor work. The copy constructor works when there is nothing inside of it, but I know that I need to assign values within this. Any helpful pointers that someone might be able to give me on this would be greatly appreciated. I have been banging my head against the wall on this and can't seem to make headway, but I suspect I am not completely off base.

    Thanks!

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Matrix(int r=10, int c=10, int v=0);

    Matrix::Matrix(int r, int c, int v){

    }

    Maybe you should post what you think a constructor is supposed to do and what your constructor actually does.


    "The copy constructor works when there is nothing inside of it."

    I wouldn't say that's much of a copy constructor then. What do you think a copy constructor is supposed to do?

    Just because you have a bad book doesn't mean you have to be a slave to it. Get a good book to use as a reference when your bad book fails you. I suggest: "Ivor Horton's Beginning C++".
    Last edited by 7stud; 05-22-2003 at 10:20 PM.

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    My understanding of a constructor (with the disclaimer that I may be off base), is that it's part of a class that essentially initializes standard parameters of the class. I think that my constructor will only work if a person creates an instance of the class without any arguments. For instance, if I entered:

    Code:
    Matrix A;
    It should make A an instance of the Matrix class with the rows (r) set to 10 and columns(c) set to 10 and initial value of the matrix (v) as 0 (10 by 10 grid set all to 0's).

    Like I said though, this could be very far off...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21