class function in .h header file

Hello my friends
I wrote a code to build a matrix class. my code is in the united type and being run as well. I want to separate it into main.cpp , matrix.h and matrix.cpp ones. when I try to do that give me the error "the vector does not name a type"
please someone can do it for me
the best
Mohsen


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<iomanip>
#include<vector>



using namespace std;
class matrix
{
private:
    unsigned rowSize,colSize;
    vector<vector<double> > my_matrix;
public:
    matrix(unsigned ,unsigned, double);
    void print();
};
matrix::matrix(unsigned rowSize,unsigned colSize, double initial):rowSize(rowSize),colSize(colSize)
{
    unsigned M_size = rowSize;
    unsigned N_size = colSize;
    my_matrix.resize(M_size);
    for(unsigned i=0;i<M_size;i++)
    {
        my_matrix[i].resize(N_size,initial);
    }

}
void matrix::print()
{
    for(unsigned i=0;i<rowSize;i++)
    {
        for(unsigned j=0;j<colSize;j++)
        {
          cout<<setw(3)<<my_matrix[i][j];
        }
        cout<<endl;
    }
}


int main()
{
    unsigned M=4;
    unsigned N=4;
    matrix p1(M,N,0.0);
    p1.print();




}

Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code much easier.
http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add the code tags.

Which member function are you trying to put into a .cpp file? And what is the error you are getting? Simply saying you are getting an error doesn't help us to help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef matrix_h
#define matrix_h

#include <vector> // this is the only thing we depend on in this header

class matrix
{
private:
    std::vector<std::vector<double> > my_matrix;
public:
    matrix(unsigned ,unsigned, double);
    void print() const;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include "matrix.h"

// The vector has useful constructors
matrix::matrix( unsigned rowSize,unsigned colSize, double initial )
: my_matrix(rowSize, std::vector<double>(colSize, initial))
{
}

void matrix::print() const
{
    for ( auto& row : my_matrix )
    {
        for ( double x : row )
        {
          std::cout << std::setw(3) << x;
        }
        std::cout << '\n';
    }
}


1
2
3
4
5
6
7
8
9
#include "matrix.h" // this is the only thing we depend on in main.cpp

int main()
{
    unsigned M=3;
    unsigned N=4;
    matrix p1(M,N,0.0);
    p1.print();
}


However, why use 2D, when you can use 1D:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class matrix
{
private:
    unsigned rowSize,colSize;
    vector<double> my_matrix;
};

matrix::matrix( unsigned rowSize,unsigned colSize, double initial )
: rowSize(rowSize), colSize(colSize),
  my_matrix( rowSize * colSize, initial )
{
}

void matrix::print()
{
    for (unsigned row=0; row<rowSize; ++row )
    {
        for (unsigned col=0; col<colSize; ++col )
        {
          std::cout << std::setw(3) << my_matrix[ row*colSize + col ];
        }
        std::cout << '\n';
    }
}
thank u keskiverto
but your code give me error in code block too
the same error as mine code

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\f\Documents\main.o:main.cpp|| undefined reference to `matrix::matrix(unsigned int, unsigned int, double)'|
C:\Users\f\Documents\main.o:main.cpp|| undefined reference to `matrix::print() const'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Build file: "no target" in "no project" (compiler: unknown)

That "no project" is probably your problem. You need to create a project and add all the files to that project, then compile that project.

Last edited on
That is a linker error. Linker should combine object codes to an executable, but it was not given all objects. Both main.cpp and matrix.cpp has to be in the build.
Topic archived. No new replies allowed.