简体   繁体   中英

using an abstract class from another header file in c++

I have 3 files

dimentions.h:

namespace mtm { 
    class Dimensions {
        int row, col;
        //some code
    };
}

IntMatrix.h:

#include "dimentions.h"
namespace mtm 
{ 
    class intMatrix
    {
    private:
        int** data;
        int col;
        int row;

    public:
        intMatrix(Dimensions dims, int num=0);
        //some code
    };

    //the line bellow is where I get the error
    intMatrix(Dimensions dims, int num=0): col(dims.getCol()),row(dims.getRow()) ,data(new int*[dims.getRow()])
    {
        for(int i=0;i<dims.getCol())
        {
            data[i](new int[dims.getCol]);
        }
        for(int i=0;i<dims.getRow();i++)
        {
            for(int j=0;j<dims.getCol();j++)
            {
                data[i][j]=num;
            }
        }
    }
}

the compiler says: expected ')' before 'dims' and when I put the mouse at dims, vs code says: " error-type mtm::dims" but dims is not a type it is a vriable.

IntMatrix.cpp:

#include "IntMatrix.h"
using namespace mtm;
//some code

in IntMatrix.cpp the problem is that it doesn't recognize what Dimentions is, however it does recognize what intMatrix is.

Welcome to StackOverflow!

Compiler messages are sometimes misleading, and in your case you have an error because you are repeating the default value in your implementation. This is probably the error the compiler is complaining about.

EDIT As pointer out by drescherjm, you also forgot the add the class name to the constructor

The correct definition should be:

intMatrix::intMatrix(Dimensions dims, int num): ...

Please let me know if you still have the error after that.

In your C++ source code the constructor must be defined as

intMatrix:: intMatrix(Dimensions dims, int num)....

So you have two mistakes: you have to add intMatrix and delete =0

But do not ever write a matrix class like this. This is terrible C++ code. You should never need to call new directly. And your data, in this case, is laid down very cache unfriendly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM