I have written this program but it doesn't work. It gives an error that x and y was not declared and expected primary expression before int on line 17.
#include<iostream>
using namespace std;
class shapes
{
int width, height;
public:
int getvalue();
void decideshape(int l, int b);
};
main()
{
cout<<"to find what type of shape you have input the measurements"<<endl;
shapes toy;
toy.getvalue();
toy.decideshape();
}
int shapes::getvalue()
{
int l, b;
cout<<"length = ";
cin>>l;
cout<<"breath = ";
cin>>b;
}
void shapes::decideshape(x, y)
{
if(x==y)
cout<<"This is square"<<endl;
else
cout<<"This is rectangle"<<endl;
}
how should i return 2 values from function getvalue
Arguments are required to have types in C++. Write your definition of shapes::decideshape as
void shapes::decideshape(int x, int y)
You don't return a value from shapes::getvalue.
You pass too few (actually none) parameters to shapes::decideshape. Two ints are expected to be supplied.
You need to tell the compiler what a function returns explicitly. Add the int return value to main.
You are missing the type of x and y in the parameter list:
void shapes::decideshape(int x, int y)
Related
i want to take an input from c++ function and return it to the main function
, i've already tried to do it but the function returns zero , any idea ?
#include<iostream>
using namespace std;
int input( int x);
int main()
{
int number;
input(number);
cout<<number;
}
int input (int x)
{
cin>>x;
return x;
}
you need to pass by reference
void input (int & x)
{
cin>>x;
}
or use the return value
int main()
{
int number;
number = input();
cout<<number;
}
int input ()
{
cin>>x;
return x;
}
You can change your function to make it take x by reference:
void input (int& x)
{
cin>>x;
}
This way, you don't even need to return x, because your function will update its value as it is passed by reference.
So, let's get it from the begining :
It any function, all its variables are temporary and only accessible in this function's scope unless you use a pointer or a reference(c++ only)
So what happen when you call your function is that a copy of number's is created into x
Also, the return statement of a function is used to... return values! Yeah, weird uh?
So actually you don't even need to send any parameter to your function and just take its result :
#include<iostream>
using namespace std;
int input();
int main()
{
int number = input(); // Takes what the return statement gives
cout<<number;
}
int input ()
{
int x;
cin>>x;
return x;
}
Here's another way by using c++'s references :
#include<iostream>
using namespace std;
void input( int& x);
int main()
{
int number;
input(number);
cout<<number;
}
int input (int& x)//Takes number's address
{
cin>>x;
}
#include <iostream>
using namespace std;
template<class Type>
void Knapsack(Type *v,int *w,int c,int n,Type **m)
{
int i,j;
int jMax=max(w[n]-1,c);
for(j=0;j<=jMax;j++)
m[n][j]=0;
for(j=w[n];j<=c;j++)
m[n][j]=v[n];
for(i=n-1;i>1;i--)
{
for(j=0;j<=w[i]-1;j++)
m[i][j]=m[i+1][j];
for(j=w[i];j<=c;j++)
{
m[i][j]=max(m[i+1][j],m[i+1][j-w[i]]+v[i]);
}
}
m[1][c]=m[2][c];
if(c>=w[1])
m[1][c]=max(m[2][c],m[1][c-w[1]]+v[1]);
}
template <class Type>
void TrackBack(Type **m,int *w,int c,int n,int *x){
for(int i=1;i<=n;i++)
{
if(m[i][c]==m[i+1][c])
x[i]=0;
else
x[i]=1;
}
}
int main()
{
int m[101][101]={0};
int x[101];
int n=5;
int c=10;
int w[5]={2,2,6,5,4};
int v[5]={6,3,5,4,6};
Knapsack(v,w,c,n,m);
return 0;
}
I an writing the algorithm of 01 Knapsack problem.
my Xcode says "No matching function for call to 'Knapsack' "
I am stumbled by the red alarm.
I'm confused for passing arguments.
Is there anyone can help me? Thanks a lot:)
This is not a valid conversion:
int m[101][101]
...
Knapsack(v,w,c,n,m);
// ^-- expects a Type **m
m can decay to type "pointer to array of 101 ints", but no further.
At least type of argument
int m[101][101]={0};
is not equivalent to T ** where T is int.
When this array is passed by value as an argument it is implicitly converted to pointer to its first element and has type int ( * )[101]
Take into account that in any case this function is invalid. For example argument for second parameter w has 5 elements.
int w[5]={2,2,6,5,4};
The valid range of indices for it is [0,4]. n in the function call is equal to 5.
So this statement
int jMax=max(w[n]-1,c);
has undefined behaviour because you are using inadmissible index equal to 5.
template<class Type>
void Knapsack(Type *v,int *w,int c,int n,Type **m)
{
int i,j;
int jMax=max(w[n]-1,c);
//...
You a little bit wrong with definition of functions
template < class Type>
void Knapsack(Type *v,int *w,int c,int n,Type m[][101])
template < class Type>
void TrackBack(Type m[][101],int *w,int c,int n,int *x)
I've this error when I try to save a number into my vector...
Invalid types ‘<unresolved overloaded function type>[int]’ for array subscript
The code is:
class Elemento{
private:
int Nodo;
public:
Elemento(){};
~Elemento(){};
void SetNumero(int x) { Nodo = x; };
int GetNumero() { return Nodo; };
};
class MagicSquare{
private:
int N;
int Possibili_N;
int Magic_Constant;
vector<Elemento> Square(int Possibili_N);
public:
MagicSquare() { };
~MagicSquare() { };
void Set_N(int x) { N = x; };
void Set_PossibiliN(int x) { Possibili_N = x; };
void Set_MagicConstant(int x) { Magic_Constant = x; };
. . .
void SetSquare(int i, int x) { Square[i].SetNumero(x); }; // got error here
int GetSquare(int i) { return Square[i].GetNumero(); }; // got error here
};
I've got error whenever I use Square[i].method()...
I call a method that pass the index in the Square and the value to put in Elemento->Nodo, but I've to use a public method to access to private Nodo. The same with the GET. I want to get the value for displaying it.
You seem to have declared Square as a function, not a variable.
Instead, declare vector<Elemento> Square; and initialize it in the constructor.
You declared Square as a function, not a variable. So Square[i] is not valid.
Change
vector<Elemento> Square(int Possibili_N);
to
vector<Elemento> Square;
or call it using
Square(i)
if it is actually a function.
If you change it to a variable, you need to be sure to initialize it properly, preferably in the constructor.
Your line vector<Elemento> Square(int Possibili_N); is know as C++ most vexing parse.
Instead of declaring a member variable, as intended, you are declaring a function taking an int and returning a vector.
Instead, setup the member vector (and all other member variables) in the constructor initialization list:
class MagicSquare{
private:
int N;
int Possibili_N;
int Magic_Constant;
vector<Elemento> Square;
public:
MagicSquare( int n, int p, int m ) :
N( n ),
Possibili_N( p ),
Magic_Constant( m ),
Square( p ) {
}
...
I am curious about how to call function pointer in a map structure. Here is the details:
#include<iostream>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
class FuncP;
typedef int(FuncP::*func) (int, int);
class FuncP
{
public:
map<int, func> fmap;
map<int, string> fstring;
public:
FuncP(){}
void initial();
int max(int x, int y);
int min(int x, int y);
int call(int op, int x, int y)
{
return (this->*fmap[op])(x, y);
}
};
void FuncP::initial()
{
fmap[0] = &FuncP::max;
fmap[1] = &FuncP::min;
fstring[0] = "fdsfaf";
}
int FuncP::min(int x, int y)
{
return (x<y)?x:y;
}
int FuncP::max(int x, int y)
{
return (x<y)?y:x;
}
int main()
{
func h = &FuncP::max;
FuncP *handle = new FuncP();
handle->initial();
cout<< handle->call(0, 1, 4); //1
cout<< (handle->FuncP::*fmap)[0](1,5); //2
return 0;
}
For the number 2 (handle->FuncP::*fmap)0; The compiler gives a error:
‘fmap’ was not declared in this scope
I am not sure why it happened. What the difference of the number 1 and 2 call methods?
As commented by Piotr, a correct way would be
(handle->*(handle->fmap[0]))(1, 5);
Explanation:
handle->fmap[0] gives you the function pointer. To call it, you need to dereference it, giving *(handle->fmap[0]) (parentheses optional)
and call it on the respecting object (handle), leaving us with the expression above.
This is essentially the same as your above statement (this->*fmap[op])(x, y) except of handle->fmap[0]instead offmap[op].
My program simply is to increase the salary int the emp class throw the the function increase
but I'm having this error int the call function line from the line :
No suitable constructor to convert from int to emp
here 's my code :
#include <iostream>
#include <string>
using namespace std;
class emp
{
public:
int salary;
};
void increase(emp x,emp y)
{
x.salary+=100;
y.salary+=250;
}
int main()
{
int value=0;
emp fst, scnd;
cin >> fst.salary >> scnd.salary;
increase(fst.salary,scnd.salary);
cout << fst.salary << endl << scnd.salary << endl;
cin >> value;
return 0;
}
increase expects two emps as parameters, yet you pass in two ints.
Change
increase(fst.salary,scnd.salary);
to
increase(fst,scnd);
Your next question is going to be why the values don't change, so to save you the trouble - it's because you're passing by value, effectively changing copies of your original objects. You'll need to pass by reference:
void increase(emp& x,emp& y)
increase(fst.salary,scnd.salary); should be increase(fst,scnd);, void increase(emp x,emp y) ... should be void increase(emp& x,emp& y) ...
You need to pass emp not int. Further, you are passing parameters by value. Use this instead:
void increase(emp &x,emp &y)
And pass the struct variables; i.e. fst and scnd instead of fst.salary and scnd.salary. Refer this question for better understanding.