LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2009, 04:38 PM   #1
holystinken
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
invalid conversion from `const char*' to `char' error


I am trying to get my code to work for chars it works for ints but when I try to use it for chars it gives me the error invalid conversion from `const char*' to `char' here is my code

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iterator>
using namespace std;



class mystack
{
public:
int size;
char* stacker;
int topg;
char a;
char data;
mystack(){
topg = -1;
size = 1;
stacker =new char[size];
}
~mystack(){
if(stacker) delete[] stacker;
}
char push(char a){

size++;
topg++;
stacker[topg] = a;
return 0;
}
char pop()
{
char data = stacker[topg];
topg--;
cout << data << endl;
return data;
}
int top(){ return topg;}

};

int main()
{
mystack a;
a.push("a");
a.push();
a.pop();
cout << a.top() << endl;
system("pause");
}

I just need to understand why exactly i am getting this error and what I can do to change it
 
Old 11-22-2009, 04:43 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Next time highlight the line that the compiler is complaining about (yes, it does give you a line). Otherwise we have to manually dig through it, like I just did.

Your function is declared as
Code:
char push(char a)
but your calling it as
Code:
a.push("a");
You need to pass a char, not a string (i.e. char*). "a" is a string but 'a' is a char.
 
Old 11-22-2009, 04:45 PM   #3
holystinken
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry about that I will definatly rememebr for next time thanks for the quick reply though
 
Old 11-22-2009, 05:14 PM   #4
holystinken
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ccould you elaborate a little more because I have tried changing my function char push(char a) to just char push() and other things as well with no success I am still kind of lost.
 
Old 11-22-2009, 05:16 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Your function definition is fine, the problem is with how your calling it, as I said above.

Your doing
Code:
a.push("a");
but you need to do
Code:
a.push('a');
.

Also remove the line after your first push, as it will give an error. That is, remove this
Code:
a.push();
 
Old 11-22-2009, 05:26 PM   #6
holystinken
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Oh wow see I thought I had tried that but, I guess not because it did work thank you so much for your help I really apperciate it
 
Old 11-22-2009, 11:20 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Please click the blue hand button on the right bottom corner of any post that has helped you.
 
Old 11-23-2009, 06:01 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Ya, good idea
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with "error: invalid conversion from `const char*' to `char*' " dave.f.one Linux - Newbie 11 02-11-2015 07:17 AM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
invalid conversion from `const char*' to `char*' deepinlife Programming 22 08-05-2006 10:49 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration