Hello, i have a easy problem for u guys =). I have searched the webb but can't find a solution for it. How do i solve this kind of problems, Sorry for the spelling as usual ;)?
Text::Text(const char ch)
{
strcpy(m_str,ch);
}
m_str is a char *.
ch is a const char ch, as u can see.
error C2664: 'strcpy' : cannot convert parameter 2 from 'const char' to 'const char *
.h file
#include <iostream>
#include <cassert>
#include <cstring>
class Text{
public:
Text::Text(const char * = "");
Text::Text(const Text & );
Text::Text(const char ch);
~Text(){delete [] m_str;}
void Text::print(std::ostream &out)const;
//void copy(const Text &rhs);
const Text &Text::append(const Text & );
const Text &Text::operator =(const Text &rhs);
private:
char *m_str;
int m_len;
};
//cpp file
#include "Text.h"
Text::Text(const char *rhs) : m_len(strlen(rhs))//default constructor converts char * to string
{
m_str = new char [m_len + 1];
assert(m_str != 0);
strcpy(m_str, rhs);
}
Text::Text(const char ch)
{
strcpy(m_str,ch);
}
Text::Text(const Text &rhs) : m_len(rhs.m_len)
{
m_str = new char [m_len + 1];
assert(m_str != 0);
strcpy(m_str, rhs.m_str);
}
void Text::print(std::ostream &out) const
{
out<<m_str<<std::endl;
}
const Text &Text::operator =(const Text &rhs)
{
if(&rhs != this){
m_len = rhs.m_len;
delete [] m_str;
m_str = new char [m_len + 1];
assert(m_str != 0);
strcpy(m_str, rhs.m_str);
}
else
std::cout<<"Självdeklarering av sträng!"<<std::endl;
return *this;
}
const Text &Text::append(const Text &rhs)
{
char *tempptr = this->m_str;
this->m_len += rhs.m_len;
m_str = new char[this->m_len +1 ];
assert(m_str != 0);
strcpy(m_str,tempptr);
strcat(m_str,rhs.m_str);
delete [] tempptr;
//std::cout<<"mjaha hur ska det här gå";
return *this;
}
int main()
{
char vanta;
Text q('a');
// Text t("strangen funkar tror jag");
//Text s("den här:");
//Text h(":hatar strangar!!!!!!!");
////s.copy(t);
///* s = t;*/
// s.append(t);
//s.append(h);
/* t.print(std::cout);
s.print(std::cout);
a.print(std::cout);
s = "ny sträng av ngt slag";
s.print(std::cout);*/
q.print(std::cout);
std::cin>>vanta;
return 0;
}