Method not a static member of a class - c++

Hello guys i had this error while i was compiling
error:'unisgned int vehicle::accelerate' is not a static member of 'class vehicle'
Any idea how to fix this?
Header file
class vehicle
{
public:
enum Switch
{
SWITCH_ON=0,
SWITCH_OFF
};
vehicle();
~vehicle();
bool powerSwitch(Switch );
unsigned int accelerate(unsigned int );
unsigned int decelerate(unsigned int );
bool isMoving();
unsigned int getSpeed();
unsigned int setSpeed(unsigned int);
private:
unsigned int speed;
bool moving;
};
vehicle.cpp
unsigned int vehicle::accelerate(amount)
{
if(moving==true;){
speed+=amount;
}
return speed;
}

You are missing the type in the parameter list:
unsigned int vehicle::accelerate(unsigned int amount)
{
.....
}

As you have declared:
unsigned int accelerate(unsigned int );
So you must implement:
unsigned int vehicle::accelerate(unsigned int amount)
{
//...
The type needs to be given again at this point.

The error might be somewhere else, where you're trying to access the accelerate member not using the operator -> or ., but ::, apart from forgetting the type of the parameter

Related

Passing const variable between member functions to be index of array

I have tried to solve this, but I can't. I have a class definition and I want a member function (siz) to return a constant value to another member function (abc). This value is used as maximum index in an array declaration in that function. But this doesn't seems to work. Here is a simplified version:
class bin {
constexpr int siz();
public:
void abc();
};
constexpr int bin::siz() {
const int sizz = sizeof(int) * 8;
}
void bin::abc() {
char arr[siz()]; // compiler: this expression didn't evaluate as constant (¿?)
};
However, this other very similar code (but using simple functions) does compile...
constexpr int siz() {
const int sizz = sizeof(int) * 8;
return sizz;
}
int main() {
char arr[siz()];
return 0;
}
I am not entirely sure but I think the problem is that in bin::abc, the object can be anything at run time. Hence, bin::siz() cannot be evaluated at compile time.
The following works fine
int main()
{
bin b;
char arr[b.siz()];
}
after changing bin to:
class bin {
public:
constexpr int siz();
};
constexpr int bin::siz() {
return sizeof(int) * 8;
}
If siz does not depend on the state of the object, as in your posted code, I suggest making it a static member function.
The following works fine for me.
class bin {
public:
static constexpr int siz();
void abc() const;
};
constexpr int bin::siz() {
return sizeof(int) * 8;
}
void bin::abc() const {
char arr[siz()];
}
int main()
{
bin b;
char arr[b.siz()];
}

cannot convert int to int c++

I'm trying to overload operator "<<" in c++. I have a class called TCalendario, with 3 privates int attributes, and 3 public getters to return this variables in public mode, but when I try to save the return value of the getter in another int variable, i can't compile. The mensage error is:
"cannot convert 'TCalendario::Dia' from type 'int (TCalendario::)()' to type 'int'"
Private attributes:
private:
int dia, mes, anyo
And getters:
int TCalendario::Dia()
{
return dia;
}
int TCalendario::Mes()
{
return mes;
}
int TCalendario::Anyo()
{
return anyo;
}
The code that crashes when I try to compile is:
ostream& operator<<(ostream &x, TCalendario &c)
{
int day=c.Dia;
int month=c.Mes;
int year=c.Anyo;
}
In order to call a method without arguments, you need to put empty braces, like this:
int day=c.Dia();
You missed the () in function call
int day=c.Dia();
int month=c.Mes();
int year=c.Anyo();

Const Static Function Pointer in Class ~ How to initialize it?

I have this small piece of code:
int add(int x, int y)
{
return x + y;
}
class A
{
public:
const static int (*FP)(int, int) = &add;
};
int main()
{
int x = 3;
int y = 2;
int z = A::FP(x, y);
return 0;
}
Under VS2012 this generates the following error:
error C2864: 'A::FP' : only static const integral data members can be initialized within a class.
Is there something I am not seeing? Or is it plainly not possible for some reason?
Christian
Initialize outside of the class definition, using a typedef to make the constness possible:
typedef int (*func_t)(int, int);
class A
{
public:
const static func_t FP;
};
const func_t A::FP = &add;
Without the typedef the declaration:
const static int (*FP)(int, int) = &add;
is a static function pointer named FP with a return type of const int, not a const function pointer. When compiled with warnling level /W4 the following diagnostic is emitted:
warning C4180: qualifier applied to function type has no meaning; ignored
this was not immediately apparent due to the ordering of the declaration, const static int instead of static const int.
In C++03. In class initialization of non-intgeral or enum data-types is not allowed.
class A
{
public:
typedef int (*FP_ptr)(int, int);
const static FP_ptr FP;
};
const A::FP_ptr
A::FP = &add;
C++11
class A
{
public:
constexpr static int (*FP)(int, int) = &add;
};
Thanks a lot, guys. I had pretty much the same problem recently. Just want to add the answer with "const" in the right place without declaring a new type:
class A
{
public:
static int (* const FP)(int, int);
};
int (* const A::FP)(int, int) = &add;
It is okay, to change the last line to(I am not sure if it is exactly the same thing, but it works):
int (* const A::FP)(int, int) = add;
The other way (how other people solve similar issue) would be something like:
class A
{
public:
static int FP(int, int);
};
int A::FP(int x, int y)
{
return add( &x, &y );
}

c++ "no appropriate default constructor available" error using template class data member

I made a template class Grid(where i said in the header file that the default for T is float), i quoted a part of the source file:
#include"Grid.h"
template <class T>
Grid<T>::Grid(unsigned int rows=1, unsigned int columns=1)
:Rows(rows),Columns(columns)
{
reset();
}
template<class T>
Grid<T>::~Grid(){}
template <class T>
void Grid<T>::reset()
{
vector<T> vec(Rows * Columns,T());
matrix = vec;
}
And the other member functions can read/change a value of matrix or cout it.
Grid.h:
template<typename T=float> class Grid{
public:
Grid(unsigned int, unsigned int);
~Grid();
T getValue(unsigned int, unsigned int);
void setValue(unsigned int, unsigned int, T);
void reset();
void write();
private:
unsigned int Rows;
unsigned int Columns;
vector<T> matrix;
};
I found on the internet that in order to use a template class I needed to #include Grid.cpp as well as Grid.h, and doing this I can use the clas Grid and its member functions in my main(). I also put a preprocessor wrapper arround Grid.cpp.
Now, when I try to build a new class PDEProblem, without inheritance but using members from type Grid I get errors:
Error 2 error C2512: 'Grid<>' : no appropriate default constructor available c:\users\... 15
Error 3 error C2512: 'Grid<T>' : no appropriate default constructor available c:\users\... 15
4 IntelliSense: no default constructor exists for class "Grid<float>" c:\Users\... 15
PDEProblem.h:
#include"grid.h"
#include"grid.cpp"
class PDEProblem: Grid<>
{
public:
PDEProblem(unsigned int,unsigned int);
~PDEProblem();
//some more other data members
private:
Grid<char> gridFlags;
Grid<> grid;
unsigned int Rows;
unsigned int Columns;
void conPot(unsigned int, unsigned int);
void conFlag(unsigned int, unsigned int);
};
PDEProblem.cpp:
#include"grid.h"
#include"grid.cpp"
#include "PDEProblem.h"
PDEProblem::PDEProblem(unsigned int rows=1,unsigned int columns=1)
:Rows(rows), Columns(columns)
{
conPot(rows, columns);
conFlag(rows,columns);
}
PDEProblem::~PDEProblem(){}
void PDEProblem::conPot(unsigned int rows, unsigned int columns)
{
grid=Grid<>(rows,columns);
}
void PDEProblem::conFlag(unsigned int rows, unsigned int columns)
{gridFlags=Grid<char>(rows,columns);
// some stuff with a few if and for loops which sets some elements of gridFlags to 1 and the others to 0
}
How can I fix this? It seems to me that I have defaults for everything relevant?
Thank you
With my compiler (Visual Studio 2010) and your code, I can make your error go away by moving the default parameter values from the function definition to the function prototype. Specifically:
Grid.h
template<typename T=float> class Grid{
public:
Grid(unsigned int rows = 1, unsigned int columns = 1);
...
};
Grid.cpp
template <class T>
Grid<T>::Grid(unsigned int rows, unsigned int columns)
:Rows(rows),Columns(columns)
{
reset();
}
Your issue is that your main class inherits from Grid and at the same time contains two other instances of Grid. Except for the bad design, you don't have any explicit constructors for your two Grid instances, which is why you have the error.
Putting default values is not the proper way to go.

Method won't overload

I am fairly new to C++ and this method just won't overload in Vector class.
class Vector {
...
void findTriDiagonalDeterminant(mpf_t *det, unsigned long long *d, double offset) {
...
}
void findTriDiagonalDeterminant(mpf_t *det, unsigned long long *d) {
findTriDiagonalDeterminant(det, d, 0);
}
}
class Matrix : public Vector {
private:
unsigned long long* dims;
public:
void findTriDiagonalDeterminant(mpf_t* det, int index) {
Vector::findTriDiagonalDeterminant(det, dims+index);
}
...
}
The g++ says
error: no matching function for call to ‘Matrix::findTriDiagonalDeterminant(__mpf_struct (*&)[1], long long unsigned int*)’
But there's a matching function: second one from the top in Vector class.
I tried the same without pointers (by substituting *d with d and dims+index by *(dims+index)) but it didn't help.
In C++ overloads in a child class hide the methods in the parent class, so only the function in Matrix can be called with a Matrix.
You can use using to expose the extra methods, something like this.
class Matrix : public Vector {
private:
unsigned long long* dims;
public:
using Vector::findTriDiagonalDeterminat;
void findTriDiagonalDeterminant(mpf_t* det, int index) {
Vector::findTriDiagonalDeterminant(det, dims+index);
}
...
}
I'm guessing (dims+index) is getting promoted to something else. Maybe try explicitly casting the result or assign the expression to an appropriate temporary variable.