I'm currently working on a project to convert from postfix to infix using a stack implemented through a linked list. I'm trying to take in a line and then pushing each character onto a stack however I keep getting the error.
Error: invalid user-defined conversion from ‘char’ to ‘const stack_element& {aka const std::basic_string&}’
Here is my code:
#include "stack.h"
string convert(string expression)
{
stack c;
string post = " ";
for (int i =0; i<expression.length(); i++)
{
c.push(expression[i]);
}
}
int main()
{
string expression;
cout<<" Enter a Post Fix expression: ";
getline(cin,expression);
return 0;
}
and here is the push function written in another .cpp file
void stack::push(const stack_element & item)
{
cout<<"Inside push \n";
stack_node *p = new stack_node;
p->data = item;
p->next = s_top;
s_top = p;
}
do not use const as parameter in your push function
void stack::push(stack_element & item)
Change void stack::push(const stack_element & item)
to void stack::push(cons char& item) , As in your case , you are trying to create stac of characters . If you want generic use templates.
Related
I am creating a class that has two members string and int
I want to use the constructor to initialize both of these two members to use them.
#pragma once
#include <string>
#include <iostream>
using namespace std;
class donation_1
{
public:
//string name;
const char* name;
int donation_amount;
const static size_t string_size = sizeof(string);
const static size_t int_size = sizeof(int);
donation_1(char* name_1 = "Noname", int amount = 0) : name(name_1), donation_amount(amount) {};
};
int main()
{
fstream file;
file.open("donation_total1.txt", ios_base::app);
if (file.is_open())
{
donation_1("xxxx", 20).writedata(file);
donation_1("yyyy", 30).writedata(file);
donation_1("zzzz", 40).writedata(file);
donation_1("MMMM", 50).writedata(file);
donation_1("BBBB", 60).writedata(file);
file.close();
}
else
{
cout << "file couldn't be opened" << endl;
}
return 0;
}
I want to use the constructor to initialize the class variables which I will be using to update a file, however, what I am getting is this error. this error is regarding initializing the string class member.
Severity Code Description Project File Line Suppression State
Error (active) E0310 default argument of type "const char *" is incompatible with parameter of type "char *" Stream_File_Lab D:\INVSPRIVATE\C++\Projects\Stream_File_Lab\donation_1.h 17
The error message is makes it pretty clear. The variable 'name' is declared as const char* but the value being assigned to it is only char* i.e. the const-ness is missing, hence the type incompatibility error throws up.
Please, google for pointer to a const value and how to use them.
Maybe check this tutorial
I have some 15-year-old C++ code that I am trying to bring up to more modern times. At this stage, I'm trying to get code that compiled with Visual C++ 6.0 to now compile with VS 2003 (Microsoft Visual C++ .NET 69462-335-0000007-18915). If we can get this to compile cleanly & run properly, then we can take another step to get it into a more recent version of VS. But I'm having a number of problems...
Here is a snippet of the (simplified) code:
class toS
{
public:
toS() { buff[0] ='\0'; }
operator LPCTSTR() { return buff; }
protected:
void Append (TCHAR c)
{
LPTSTR p = buff + _tcslen(buff);
*p++ = c;
*p = '\0';
}
TCHAR buff[40];
};
class LtoS : public toS
{
public:
LtoS(LONG n, TCHAR c = '\0')
{
_ltot(n, buff, 10);
Append(c);
}
};
void WriteBool(const CString& Section, const CString& Key, bool Value);
CString Section;
int nLine = 0;
std::vector<bool> *BoolVect;
std::vector<bool>::iterator vi;
...
for (vi = BoolVect->begin(); vi != BoolVect->end(); vi++)
WriteBool(Section, "LineVis " + LtoS(nLine++), *vi);
...
From this I get the following error message:
error C2677: binary '+' : no global operator found which takes type 'LtoS' (or there is no acceptable conversion)
Any idea how this code ever worked? If I can find out what it did in the past, I can begin to define the overloaded operator+ to match the functionality.
Compiler error goes away when I make class tos inherit from CString with:
class tos : public CString { ... }
Hopefully this will not only compile, but will execute correctly...
Deriving from several of the comments, try adding a public conversion operator to class toS as follows:
operator LPCTSTR() const { return &buff[0]; }
You may need to explicitly construct the string in the for loop as well, e.g.:
WriteBool(Section, CString("LineVis ") + static_cast<LPCTSTR>(LtoS(nLine++)), *vi);
(Side note: As you probably know since you just extracted code for an example, there's a problem here:
std::vector<bool> BoolVect;
...
for (vi = BoolVect->begin(); vi != BoolVect->end(); vi++)
The notation you're using to access the BoolVect implies that it is a pointer, but it's not being declared as such in your example.)
Here is my declaration in question, I even use include guards:
Edit: I'm including the entire header if this will help answer any additional
questions one might have.
#ifndef STRING_H
#define STRING_H
#include<iostream>
class String
{
public:
String(const char * s = "");
String(const String & s);
String operator = (const String & s);
char & operator [] (int index);
int size();
String reverse();
int indexOf(char c);
int indexOf(String pattern);
bool operator == (String s);
bool operator != (String s);
bool operator > (String s);
bool operator < (String s);
bool operator >= (String s);
bool operator <= (String s);
String operator + (String s);
String operator += (String s);
void print(std::ostream & out);
void read(std::istream & in);
static int strLen(const String &s);
static String strCpy(const String &s, int length);
static String strDup(const String &s);
static bool strCmp(const String &s, const String &t);
~String();
private:
bool inBounds(int i)
{
return i >= 0 && i < len;
}
char * buf;
int len;
};
#endif
And here is my definition:(starting form line 183)
String String::operator = (const String & s)
{
String t(s);
return t;
}
And I keep getting this error:
>c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(183): error C2084: function 'String String::operator =(const String &)' already has a body
1> c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(11) : see previous definition of '='
can anyone offer me an explanation as to why this error occurs?
Definitions normally don't belong into header files.
You can declare and define your function inline, inside your include guards
You can use a cpp file
That said, your code looks fishy. It does not do what it seems to do. There is no assignment to this or it's variables happening. But that's a bug and not a compiler error.
I have a question concerning working with classes in c++. I must say I'm a beginner. For example, i have this class:
class student {
private:
char* name;
public:
int nrcrt;
student() {
name = new char[7];
name = "Anonim";
nrcrt = 0;
}
student(char* n, int n) {
this->name = new char[7];
strcpy(name, n);
nrcrt = nr;
}
~student() {
delete [] name;
}
char* get_name() {
return this->name;
}
}
void main() {
student group[3];
group[0] = student("Ana", 1);
group[1] = student("Alex", 2);
group[2] = student("Liam", 5);
for (i=0; i<3; i++) {
if (group.nrcrt[i] != 0)
cout << group[i].get_name() << Endl;
}
}
My question is why is it displaying different characters?
first of all your code is not working.
3.cpp:40:18: error: request for member ‘nrcrt’ in ‘group’, which is of non-class type ‘student [3]’
if(group.nrcrt[i]!=0)
i is also not declared.please make proper changes.
group.nrcrt[i]
should be changed to:
group[i].nrcrt
When the array is created, your default constructor is used.
When you assign to the elements, your destructor is called, deleting name.
The default constructor is assigning a literal to name, and deleting that memory has undefined behaviour.
In your default constructor, replace
name = "Anonim";
with
strcpy(name, "Anonim");
Your compiler should have warned you about the assignment.
If it didn't, increase the warning level of your compiler.
If it did, start listening to your compiler's warnings.
do not worry. C++ could look a bit scary as first but it is ok when you get into it. First, let's say that all classes it is good to start with upper case letters. Secondly, you have two constructors (default without parameters and one or more with, in our case one). Default consructor you need to declare an array of objects:
Student group[3];
The next important thing is that you then do not need the rest of the constructors in that case.
group[0]=student("Ana",1);
group[1]=student("Alex",2);
group[2]=student("Liam",5);
Remember to include ; at the end of class declaration. To put all the statements and expression throughout your interation within the same loop. Here is what I found as an errors anf fix them. Could probably have more.
class Student
{
private:
char* name;
public:
int nrcrt;
Student()
{
name=new char[7];
strcpy(name, "Anonim");
nrcrt=0;
}
Student( char* n, int n)
{
this->name=new char[7];
strcpy(name, n);
nrcrt=nr;
}
~Student()
{
delete [] name;
}
char* get_name()
{
return this->name;
}
};
int main()
{
Student group[3];
for(int i=0;i<3;i++)
{
if(group.nrcrt[i]!=0)
cout<<group[i].get_name()<<endl;
}
return 0;
}
How do I implement a display function for a binary search tree of strings if display is passed as a function pointer to function inorder?
typedef void (*FunctionType)(TreeItemType& anItem);
void display(TreeItemType& anItem);
void BinarySearchTree::inorder(TreeNode *treePtr,
FunctionType visit)
{ if (treePtr != NULL)
{ inorder(treePtr->leftChildPtr, visit);
visit(treePtr->item);
inorder(treePtr->rightChildPtr, visit);
} // end if
} // end inorder
I've tried to write something like:
cout << anItem;
in the body of display but it doesn't work. TreeItemType is a string so do I need to overload the << operator or = operator to convert from TreeItemType to string? I have researched function pointers and can't figure out how to use:
visit(treePtr->item);
to display the tree's item (which is a string).
int main()
{
BinarySearchTree tree;
TreeItemType item = "20";
tree.searchTreeInsert(item);
tree.inorderTraverse(display); // call to inorderTraverse calls inorder
return 0; // indicates successful completion
} // end function main
void display(TreeItemType& anItem)
{
cout << anItem;
}
Definition of TreeItemType:
typedef string TreeItemType;