Dear All,
I am trying to make class which uses an array to give a periodic function and am having some trouble!
Effectively, I am trying to make an array such that, for any point (i,j) in a 2D matrix, of size LxL, the command "next_i[i]" will return "(i+1)%L", enabling the selection of a point to the right of any point in the matrix (next[i], j)
I have tried adapting some code given to us in lectures ("Matrix(i,j)").
Here's my code:
Code:
#include <vector>
class Matrix
{
private:
std::vector<double> m_data;
int m_rows;
int m_cols;
public:
Matrix( int n_rows, int n_cols, int init = 1 )
: m_data( n_rows * n_cols, init ),
m_rows( n_rows ), m_cols( n_cols ) {}
double& operator()(int i, int j)
{
return m_data[i*m_cols + j];
}
/*Start of my coding*/
public:
next_i( int n_cols )
: m_data(n_cols),
m_cols(n_cols) {}
double& operator()(int i)
{
m_data[i] = (i + 1)% m_cols;
return m_data[i];
}
/*End of my coding*/
};
And here's what the compiler is telling me:
pccaths25:Testing$ g++ -g -pedantic -Wall $PT2_INC -o test.exe test.cc $PT2_LIB
In file included from test.cc:1:
Matrix2.h:23: error: ISO C++ forbids declaration of ‘next_i’ with no type
Matrix2.h: In member function ‘int Matrix::next_i(int)’:
Matrix2.h:24: error: only constructors take base initializers
Matrix2.h:25: warning: no return statement in function returning non-void
pccaths25:Testing$
I am new to c++ and am not quite sure what this is telling me - suggestions would be greatly appreciated. Thanks