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;




}

>I have searched the webb but can't find a solution for it.
Then how do you know it's an easy problem? My guess is that you didn't look very hard, because the answer can be found easily.

>ch is a const char ch, as u can see.
Yes, I can see. I can also see that strcpy expects a pointer to const char for the second argument. Oh, and just saying &ch probably won't do what you want, since I get the feeling that's the next thing you'd try. :icon_rolleyes:

In your first constructor you are using a function, strcpy() to copy the contents of one string to another. Which works great because strcpy() takes two strings as arguments.

In your second constructor you want to take a char and make a string (an array of chars) out of it. None of the string functions are going to work for you because a char is not a string.

You need to find another function that will upgrade a char into a string. My suggestion is to start by reading about sprintf() and going from there.

The second question you asked was how to solve this kind of problem. Wikipedia is an excellent resource for intro level C questions.

hum i fixed the problem, it was quite easy actually =) it took me 2 days to fix the problem.

Text::Text(const char ch)
{ 
//new func

        char *temp = new char[2];
        m_str = new char[2];
        temp[0]= ch;
        temp[1]= '\0';

            strcpy(m_str,temp); 

}

compare to

Text::Text(const char ch)
{
//old func
    strcpy(m_str,ch);

}

> char *temp = new char[2];
char temp[2];
would have worked as well, plus it has the advantage of not being a memory leak.

Yep, that worked well =).

> char *temp = new char[2];
char temp[2];
would have worked as well, plus it has the advantage of not being a memory leak.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.