I am getting an error while writing a constructor which has some arguments to be passed. Searched previous threads but I am not getting it cleared.
Here is my code :
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int curr_state;
//int signal[3],trans_in[7],trans_fin[7],trans_sig[7];
class state_machine
{
private :
int start,end;
public :
state_machine(const int Q[],const int signals[],const int trans[][10][10],const int start,const int end);
// int mystate();
int run();
int get_signal( );
int get_state();
int change_state();
};
state_machine::state_machine(const int Q[],const int signals[],const int trans[][10][10],const int start,const int end)
{
}
int main(int argc, char * argv[])
{
state_machine s;
int Q[5]={1,2,3,4,5};
int signals[3]={0,1,2};
int trans[][3][1]={{1,0,2},{1,1,0},{1,2,0},{2,0,3},{2,1,0},{2,2,0},{3,0,5},{3,1,4},{3,2,2},{4,0,2},{4,1,0},{4,2,0},{5,0,0},{5,1,0},{5,2,0}};
int start=1,end=5;
//s.state_machine(Q,signals,trans,start,end);
}
Getting output as :
state_machine.cpp: In function ‘int main(int, char**)’:<br>
state_machine.cpp:27:16: error: no matching function for call to ‘state_machine::state_machine()’<br>
state_machine.cpp:21:1: note: candidates are: state_machine::state_machine(const int*, const int*, const int (*)[10][10], int, int)<br>
state_machine.cpp:8:1: note: state_machine::state_machine(const state_machine&)
The constructor for state_machine expects an int trans[][10][10]. This must be matched by the type of the trans array in main.
You must change your main to
int main(int argc, char * argv[])
{
int Q[5]={1,2,3,4,5};
int signals[3]={0,1,2};
int trans[][10][10]={{1,0,2},{1,1,0},{1,2,0},{2,0,3},{2,1,0},{2,2,0},{3,0,5},{3,1,4},{3,2,2},{4,0,2},{4,1,0},{4,2,0},{5,0,0},{5,1,0},{5,2,0}};
int start=1,end=5;
state_machine s(Q,signals,trans,start,end);
return 0;
}
Although the initializer list for trans doesn't fit the required type.
Your 3D arrays sizes that you specified needs to match exactly, which it does not. As the error says it is expecting const int (*)[10][10]
if int trans[][3][1] became int trans[][10][10] this error will go away
First of all you cannot create state_machine s; as no body for ctor w/o parameters are available.
Secondly, you need to call last line as state_machine s(Q,signals,trans,start,end);
Thirdly, 3rd parameter passing for ctor is incorrect. for 3d array, indexes should match. make int trans[][3][1] to int trans[][10][10] or vice-versa.
Related
While messing around with the type syntax, I noticed this is legal :
typedef int *((* T)[10]);
T fun(){
return 0;
};
int main(int argc, char * argv[]){
//int c = fun(); // (1)
return 0;
}
...And if you uncomment (1), then you get an error message of this kind (GCC / Clang) : "error: cannot initialize a variable of type 'int' with an rvalue of type 'T' (aka 'int *((*)[10])')" (Normal so far). Notice however the "aka" that points out the type is an alias of int *((*)[10]) and not simply int ***
However, It seems impossible to declare a function with this type without using a typedef :
int *((*)[10]) fun(){ // The compiler does not approve
return 0;
};
int *((* fun2)[10]) (){ // The compiler does not approve either
return 0;
};
int main(int argc, char * argv[]){
//int c = fun(); // (1)
return 0;
}
...Then I was wondering why ?
(the question is for the C language, but it looks like it's the same for C++)
This type:
typedef int *((* T)[10]);
Is a pointer to an array of size 10 whose members are of type int *. This is not the same as an int ***.
As for creating a function that returns this type, you would need this:
int *(*fun())[10] {
return 0;
};
But using a typedef makes this much clearer.
int *((*fun())[10]) {
return 0;
};
... Yup. You should probably stick to the typedef for the sake of readability :)
The original
typedef int *((* T)[10])
can shed the outer parens:
typedef int *(* T)[10]
Or aligned with dbush's function:
typedef int *(* T )[10]
int *(* fun() )[10]
I am new to c++ & don't know the basics all that well. pls help (sorry if the solution to this is already available, but I couldn't find any)
This is the Error I am getting:
expected primary-expression before ‘]’ token
char CusName[50]=x[];
^
For this code below:
#include <iostream>
using namespace std;
class BankAccount
{
private:
char CusName[50];
char CusId[10];
float accBalance, dep, witd;
public:
void setCusDetails(char x[], char n)
{
char CusName[50]=x[];
}
};
int main()
{
BankAccount customer1;
char cus1Name[50];
cin>>cus1Name;
customer1.setCusDetails(cus1Name, 50);
return 0;
}
Your char array looks like a string. Try using std::string instead and prefer using const references for function parameters.
If you want to use char arrays, and if your point was to copy a null-terminated string by value, then use functions like strncpy.
Using std::string may be easier for you to hide the burden of memory allocation and discover the language step by step.
You can instead use string to input and pass values.
#include <iostream>
using namespace std;
class BankAccount
{
private:
string CusName; //CusName of type string
char CusId[10];
float accBalance, dep, witd;
public:
void setCusDetails(string str, char n) //parameter str of type string
{
CusName=str; //Assign it to the already declared 'CusName' variable.
}
};
int main()
{
BankAccount customer1;
string cus1Name;
cin>>cus1Name;
customer1.setCusDetails(cus1Name, 50);
return 0;
}
Below is a piece of code I was trying to run where I have function I want to run (dN) within my main function which returns a value of type complex<double>.
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1(0.0,1.0); //imaginary number definition
class Functions{
public:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
int main(int argc, const char * argv[]) {
//...more code here
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
Although this code does not run and returns the error: Call to non-static member function without an object argument
Based on my understanding, C++ does not allow nested functions and my code above would not work because I call a separate function in my main function. Although (based on the link) it does appear one can implement "local classes" by defining a function within a struct which would have to be inside the main function. Although when I try doing that:
int main(int argc, const char * argv[]) {
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
struct Functions{
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
the error persists. Ultimately, I am simply wanting to use the function dN that returns output of type complex<double> within my main function, but am unsure of the best/operational way to implement this.
I believe you misunderstand what a nested function is.
A nested function would look like this:
int main()
{
void nested() {} // not allowed in C++
}
The solution to your problem is in the error message provided by your compiler:
Call to non-static member function without an object argument
Take a look at the following:
// Example 1
struct Functions {
void func() {}
};
int main()
{
// to call Functions::func() you would need to have an object
// of type Functions because Functions::func() is not a static function
Functions f;
f.func();
}
// Example 2
// by making func() static you can call it without an object:
struct Functions {
static void func() {}
};
int main()
{
Functions::func(); // OK
}
Ultimately, I am simply wanting to use the function dN that returns output of type complex within my main function, but am unsure of the best/operational way to implement this.
Use a free function, like main is, unless dN has a specific reason to be part of a class:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1)
{
...
}
int main(int argc, const char * argv[]) {
...
//like this
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
//not like this
//complex<double> dN_OUT = dN(**NM,1,20,0.,20.);
}
Option 1:
You can do this with class like below
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1 (0.0, 1.0); //imaginary number definition
class Functions {
public:
complex<double> dN (complex<double> **N, int k, int i, complex<double> kN, double T1)
{
complex<double> OUT = Im1*(N[k][i] + kN) / (T1);
return OUT;
};
};
int main (int argc, const char * argv[]) {
//...more code here
complex<double> **NM = new complex<double>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<double>[500];
}
Functions fun; //create class instance
//call the function NOTE the changes here i.e not correct passing **NM
complex<double> dN_OUT = fun.dN (NM, 1, 20, 0., 20.);
return 0;
};
Option 2 (mentioned by others direct calling with changes instead of **NM you should use NM.
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
...
}
int main(int argc, const char * argv[]) {
...
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
}
Hi I am new in c++ and I make skeleton of program and I have some problems with destructors and constructors.
My head.cpp:
#include <iostream>
#include "set_char.hpp"
using namespace std;
int main()
{
set_char *z1 = new set_char(unsigned char *zbior[]);
delete z1;
return 0;
};
My set_char.hpp class file:
#define ROZMIAR_MAX 256
class set_char
{
unsigned char zbior[ROZMIAR_MAX];
public:
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
int nalezy(unsigned char);
int licznosc();
void dodaj(unsigned char);
void usun(unsigned char);
};
And my set_char.cpp file:
#include "set_char.hpp"
#include <iostream>
#include <math.h>
using namespace std;
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
void set_char::dodaj(unsigned char)
{
};
void set_char::usun(unsigned char)
{
};
int set_char::nalezy(unsigned char)
{
};
int set_char::licznosc()
{
};
Among others:
you should not add any parameters in destructors:
~set_char(unsigned char *zbior[]);
^^^^^^^^^^^^^^^^^^^^^^ --- remove it
When creating set_char you should provide pointer to your array, and not the actual parameter type:
set_char *z1 = new set_char(unsigned char *zbior[]);
^^^^^^^^^^^^^^^^^^^^^^
1
You did not define your Constructor and Destructor
You declared them both in your set_char.hpp
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
However in set_char.cpp you re-declare them again without a return type. Defining a Constructor and Destructor outside of a class is illegal. Your compiler thinks they are functions and searches for a return type.
2
a Destructor may not have any arguments.
3
If you define an array as an argument in a Function or a Constructor or Destructor with brackets '[]', it may not be of variable length, thus it must be defined. If it is intended to be of variable length, it must be left out.
4
You are calling the constructor in a bad way using:
new set_char(unsigned char *zbior[]);
You already declared what arguments it takes, so hand it the arguments. A null pointer, for example.
The correct way to do it in set_char.hpp:
set_char(unsigned char *);
~set_char();
The correct way to do it in set_char.cpp:
set_char::set_char(unsigned char *zbior)
{
//Your definition
}
set_char::~set_char();
{
//Your definition
}
The correct way to do it in head.cpp:
set_char *z1 = new set_char(0x0);
Also side-note, usually using the define macro to define constants, is a C way. In C++ it is usually done with:
static const size_t ROZMIAR_MAX 256;
Second side-note. It is considered 'neater' code if you have your constants/functions and whatnot defined inside a namespace
I'm a newbie to C++, learning pointer of function recently, a little confused by usage of pointer of function;
I practiced the following code:
#include <iostream>
#include <sstream>
using namespace std;
int subtraction(int a,int b){
return a-b;
}
int main(int argc, const char * argv[])
{
int (*minus)(int,int)=subtraction;
cout<<minus(5,4);
return 0;
}
it works well;
so,I try a little variation:
#include <iostream>
#include <sstream>
using namespace std;
int subtraction(int a,int b){
return a-b;
}
int main(int argc, const char * argv[])
{
int *minus(int,int)=subtraction;//only here different!
cout<<minus(5,4);
return 0;
}
I practiced it in Xcode on Mac,it give me Error:
Illegal initializer (only variables can be initialized)
but I think compiler can recognized the two is same,why must have a pair of parenthesizes?
In your original code
int (*minus)(int,int)=subtraction;
declares minus as a function pointer that takes parameter int, int and returns int.
In your second code
int *minus(int,int)=subtraction;
declares minus as a function that takes parameter int, int and returns a pointer int *.
You can use a function name(which is automatically converted to a function pointer) to initialize a function pointer, but you can't initialize a function.
This is a matter of operator precedence. The function call operator () has a higher precedence than the dereference operator *. So you must use parentheses to specify the correct order of evaluation.
int *minus(int, int)
means: First call a function named minus, then dereference the return value (int* in this case).
int (*minus)(int, int)
means: First dereference "minus", which returns a function, and then call that function.
You have tagged your code C++ and using iostream so I can safely assume you are looking for a C++ solution.
In such scenario, its best to use class template std::function instead of the function pointer syntax that is prone to error.
#include <iostream>
#include <sstream>
#include <functional>
int subtraction(int a,int b){
return a-b;
}
int main(int argc, const char * argv[])
{
std::function<int(int,int)> minus = subtraction;
//int (*minus)(int,int)=subtraction;
std::cout<<minus(5,4);
return 0;
}
Alternatively, if you would still want to continue with pointer to function, typedefs are recommended
#include <iostream>
int subtraction(int a,int b){
return a-b;
}
typedef int (*MINUS)(int,int);
int main(int argc, const char * argv[])
{
MINUS minus = subtraction;
//int (*minus)(int,int)=subtraction;
std::cout<<minus(5,4);
return 0;
}
And finally, another widely used option is to use functors.
#include <iostream>
struct MINUS
{
int operator()(int a,int b){
return a-b;
}
};
int main(int argc, const char * argv[])
{
//int (*minus)(int,int)=subtraction;
MINUS minus;
std::cout<<minus(5,4);
return 0;
}