Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is a fairly simple program for the implementation of Stack. But there is a problem with the declaration of the push(), pop() and display() functions. The error statements are as follows. Please help me figure out the problem.
Error:
error C2664: 'push' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'pop' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'display' : cannot convert parameter 1 from 'int [10]' to 'int'
Program:
#include<iostream>
#define STACKSIZE 10
using namespace std;
void push(int,int,int);
void pop(int,int);
void display(int,int);
int main()
{
int stack[STACKSIZE],n,data,stackptr=-1;
while(1)
{
cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter a data to push: ";
cin>>data;
push(stack,stackptr,data);
break;
case 2:
pop(stack, stackptr);
break;
case 3:
display(stack, stackptr);
break;
case 4:
exit(1);
break;
default:
cout<<"\nEnter correct choice...\n\n";
}
}
system("pause");
return 0;
}
void display(int stack[STACKSIZE], int &stackptr)
{
cout<<"\n\n";
for(int i=0;i<stackptr;i++)
cout<<stack[i]<<"\n";
cout<<"\n\n";
}
void push(int stack[STACKSIZE],int &stackptr, int data)
{
if(stackptr == STACKSIZE - 1)
{
cout<<"\n\nStack full\n\n";
return ;
}
stackptr++;
stack[stackptr] = data;
}
void pop(int stack[STACKSIZE], int &stackptr)
{
if(stackptr == 0)
{
cout<<"\n\nStack Empty\n\n";
return ;
}
stackptr = stackptr-1;
}
The answer given by Gangadhar and Jansel is incomplete. You are passing values by reference so this is not enough:
void push(int stack[size],int,int);
This is what you need:
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
Your function's declarations are wrong. for example:
void push(int, int, int);
// ^^^ int
Just correct it
void push(int stack[], int&, int);
// ^^^^^^^^^^^ should be int[]
You were making mistake in function declaration. This is the correct way.
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
These error are self explanatory push,pop and display functions prototypes receives 3 int parameters, and you are trying to pass int[] by first first parameter, so, change your functions prototype to receive int[] of it first parameter.
You have a function prototype for push/pop at the start that declares an int not an array.
void push(int, int, int);
This is the version the compiler uses to check your call against. Rewrite these prototypes to have the function signature as the functions themselves.
Related
I'm trying to make a programming that would set the time to 11/7/2018, but it's not working. It only display 4 errors. Can someone please help me rectify the code.
#include <iostream>
using namespace std;
class date {
private:
int day,month,year;
public:
void advance();
date(){
day=1;
month=1;
year=2018;
};
void setDate(){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
};
void date::advance(){
for(month=1;month=<12;month++){
for(day=1;day=<31;day++){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
}
}
int main(){
date d;
cout<<"Date set as:";
d.setDate();
cout<<"Setting the advance method"<<endl;
d.advance();
return 0;
}
It display In member function void date::setDate():
[Error] expected primary-expression before '<<' token
In member function 'void date::advance()':
[Error] expected primary-expression before '<<' token
I think you meant to write
cout<<day<<"/"<<month<<"/"<<year<<endl;
instead of
cout<<date<<"/"<<month<<"/"<<year<<endl;
in both the setDate and advance functions
I am trying to use a timer to repeatedly change the PWM Output over time to have a smooth transition when the brightness changes. I keep getting this error when trying to compile the code:
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp: In constructor 'CSingleColorLight::CSingleColorLight(int)':
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp:13:58: error: cannot convert 'CSingleColorLight::DimmerCallback' from type 'void (CSingleColorLight::)(void*)' to type 'void ()(void)'
ets_timer_setfn(&Dimmer, this->DimmerCallback, NULL);
Here is my code:
class CSingleColorLight {
private:
int pin;
int intensitySetPoint;
int intensityActual;
int percentageBuffer;
ETSTimer Dimmer;
int dimmerCount;
public:
CSingleColorLight(int _pin);
bool setIntensity(int _intensity);
int getIntensity();
bool getStatus(void);
bool setStatus(bool _status);
void DimmerCallback(void*);
};
and in the cpp file:
void CSingleColorLight::DimmerCallback(void*) {
if(dimmerCount>0){
dimmerCount--;
intensityActual++;
} else if(dimmerCount<0){
dimmerCount++;
intensityActual--;
} else {
ets_timer_disarm(&Dimmer);
}
analogWrite(pin, percentageToTime[intensityActual]);
return;
}
It asks for a pointer, right? Any idea how to fix this?
Thanks a lot!
If you want DimmerCallback to take a void* argument, then you need to name it, like
void CSingleColorLight::DimmerCallback(void* x)
but you are not using the void* in the code. It looks like you should just get rid of it, so it would be
void CSingleColorLight::DimmerCallback()
int the cpp and
void DimmerCallback();
in the header.
A void* argument is a pointer that can point to any data type, it is not the same as void which is just no argument.
I am getting this error while running this code. Please look in to my code and assist me.
#include "stdafx.h"
#include <iostream>
class A
{
public:
void PrintTwoNumbers(int (*numberSource)(void))
{
int val1= numberSource();
}
int overNineThousand(void)
{
return (rand()%1000) + 9001;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int (A::*fptr) (void) = &A::overNineThousand;
int (A::*fptr1) (void);
fptr1 = &A::overNineThousand;
A a;
a.PrintTwoNumbers(&fptr); //-> how to pass here
getchar();
return 0;
}
I am fed up by searching this online, and nobody is giving a perfect solution for this.Can anybody edit this code as working code and help me?
The expected argument is a (non-member) function pointer. You instead pass a (pointer to a) pointer to member function. (Pointers to) Pointers to member functions are not convertible to pointers to (non-member) functions.
Probably the simplest solution is to fix the function argument to be of correct type, pass the implicit object argument and don't take the address of your member function pointer when you call.
void PrintTwoNumbers(int (A::*numberSource) ())
{
int val1= (this->*numberSource)();
}
a.PrintTwoNumbers(fptr);
I'm writing a c++ stack and queue implementation program, I finished the stack part, but when compiling I'm getting these errors
arrayListImp.cpp:18:19: error: expected unqualified-id
arrayList[++top]= x;
^
arrayListImp.cpp:28:13: error: 'arrayList' does not refer to a value
itemPoped=arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:35:9: error: 'arrayList' does not refer to a value
return arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:46:9: error: 'arrayList' does not refer to a value
cout<<arrayList[i]<<endl;
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
4 errors generated.
Here is the header file
#ifndef ARRAYLIST_H
class arrayList{
public:
arrayList();
static const int maxSize = 10;
int array[10];
};
class stack : public arrayList{
public:
stack();
void push(int x);
void pop();
int Top();
int isEmpty();
void print();
int x;
int top;
int itemPoped;
int i;
};
#define ARRAYLIST_H
#endif
arrayListImp.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
//Stack implementation
stack::stack(){
top = -1;
}
void stack::push(int x){
if (top == maxSize -1){
cout<<"Stack overflow"<<endl;
}
else{
arrayList[++top]= x;
cout<<x<<", is pushed on to the stack"<<endl;
}
}
void stack::pop(){
if (top == -1){
cout<<"Stack underflow"<<endl;
}
else{
itemPoped=arrayList[top];
top--;
cout<<itemPoped<<", is poped from the stack"<<endl;
}
}
int stack::Top(){
return arrayList[top];
}
int stack::isEmpty(){
if (top == -1) return 1;
return 0;
}
void stack::print(){
cout<<"Stack: "<<endl;
for (i = 0; i<=top; i++){
cout<<arrayList[i]<<endl;
}
}
arrayListUse.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
int main()
{
//Stack testing
stack S;
S.push(1);S.print();
S.push(2);S.print();
S.push(3);S.print();
S.pop();S.print();
S.push(4);S.print();
//Queue testing
return 0;
}
Can you please point out to what I'm doing wrong here?
You should just read your error messages.
You should use array instead of arrayList, which is the name of the class. So just refer to the variable instead.
The error message you got is something like
test.cpp: In member function ‘void stack::push(int)’:
test.cpp:44:18: error: expected unqualified-id before ‘[’ token
arrayList[++top]= x;
^
When you check the line, you immediately see what is wrong there.
You declare a constructor arrayList::arrayList(), but you do not define it. Either you can drop the declaration, or you should implement it in the cpp-file.
arrayList::arrayList() {
// do some initialization
}
The error message you got is something like
/tmp/cc4y06YN.o:test.cpp:function stack::stack(): error: undefined reference to 'arrayList::arrayList()'
The code could compile, but it did not link. So all declarations may be correct, but a symbol was missing. This is usually the case when you declared something you referred to, but you never defined it.
You always have written
arrayList[...]
what is the name of your class but reading the code it seems like you wanted to write
array[...]
which would access the data.
It's easier to explain with some code:
#include <iostream>
using namespace std;
struct test {
int one;
int two;
};
void insertdata(test & info)
{
cin >> info.one;
cin >> info.two;
}
int doitnow(test mytable[])
{
test info;
int i = 0;
for (i=0; i<3; i++) {
insertdata(test & info);
mytable[i] = info;
}
return i;
}
int main()
{
int total;
test mytable[10];
total = doitnow(test mytable[]);
cout << total;
}
So, I need to pass info by reference to the function insertdata, I need to use that function in doitnow to fill up a table and I need to show in the main function the number of items inserted in doitnow. I keep getting errors when I try to call functions:
teste.cpp: In function ‘int doitnow(test*)’:
teste.cpp:21:29: error: expected primary-expression before ‘&’ token
insertdata(test & info);
teste.cpp: In function ‘int main()’:
teste.cpp:33:30: error: expected primary-expression before ‘mytable’
total = doitnow(test mytable[]);
So, probably it's an obvious mistake but I'm a beginner at this.
Thanks for your help.
test& info is a definition. If you pass something to a function, you write an expression like info. It is automatically passed by reference because you specified info as a reference in the formal parameter list.
You also wrote
mytable[i] = info;
and not
mytable[i] = test & info;
didn't ya?
Use insertdata(info); instead.
Same goes for arrays. Use doitnow(test) instead.