Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
2.38/5 (3 votes)
See more:
C++
#include <iostream>
#include <string>
using namespace std;

class Mint {
public:

	Mint();
	Mint(int);
	Mint (string s);
private:
	int num[];
};
Mint::Mint()
{
	num[0] = 0;
}

Mint::Mint(int n)
{
	int i=0;
	while(n>0)
	{
		num[i] = n%10;
		n = n/10;
		i++;
	}
}
Mint::Mint(string s)
{
	int i = 0 ;
	while(s[i] != '\0')
	{
		i++;
	}
	i--;
	while(i>=0){
		num[i] = s[i] - '0';
		i--;
	}
}
int main()
{
	Mint x;
	Mint y;
	Mint z=50014;
	Mint a="065272005572"; //its important that i initialize this way  
}

when i try to compile i get an error
invalid conversion from 'const char*' to 'int' Mint a="065272005572"
and
initializing argument 1 of 'Mint::Mint(int)' [-fpermissive]
Mint(int);
any help;
Posted
Updated 21-May-23 7:12am
Comments
[no name] 26-Dec-15 9:04am    
You can use
Mint a= string("065272005572");
But this will not be the only Problem you will face. Accessing "int num[];" without "allocate" it, will be the much bigger one.... and some other ones.
Member 12223678 26-Dec-15 9:07am    
thank you but is there a way that i can initialize this way
Mint = "06214589547447"
[no name] 26-Dec-15 9:10am    
Why?
Yes there is, in case you Support Mint::Mint(const char* s)
Member 12223678 26-Dec-15 9:18am    
can you please explain what other problems that i have
Member 12223678 26-Dec-15 9:14am    
Mint::Mint(const char* s)
{
int i = 0 ;
while(s[i] != '\0')
{
i++;
}
i--;
while(i>=0){
num[i] = s[i] - '0';
i--;
}
}
something like this

Your code's got three immediate problems.

The first one (and the one the compiler's having a whine about) is that it can't work out which constructor you want called. When you give it an int it can work out you want the first called. However when you give it a const char * it has no idea that you want the string constructor called. So if you want the string constructor called actually give it a string!

C++
Mint b{ std::string{ "1234567" } };


is the sort of thing you need to use if you've got a modern compiler

C++
Mint b( std::string( "1234567" ) );


if you haven't.

Second problem is how you're storing your digits. A better way than using a built in array is to use a std::vector. It's dynamically resizable and, once sized, is usually as fast as a built in array.

The final problem is handling copying and assignment of your objects. Consider what happens when you do things like:

Mint a = 137;<br />
Mint b = a;<br />


Hopefully if you use a vector to store your digits this problem solves itself but if you try and manage memory yourself then it's something you should consider.
 
Share this answer
 
Comments
Aescleal 27-Dec-15 6:53am    
Hi Mr. Downvoter,

I like knowing where I've messed up or told people incorrect things. So if I've made a mistake, provided some level of misinformation or otherwise said something wrong could you please tell me so I either can correct this answer, explain why I disagree with you to the original poster and generally get better at what I do.

Thanks,

Ash
Gevorg Hai 28-Dec-15 16:40pm    
But this is does not meet requirement that he want to initialize by this way 'Mint a="12345"'
Aescleal 28-Dec-15 21:04pm    
The questioner didn't say it was a requirement - he just asked why the compiler gave him an error when he did it.
C++
//#include <iostream>
//#include <string>
//using namespace std;

class Mint {
public:

	Mint();
	Mint(int);
	Mint (const char*);
	~Mint();
private:
	int* m_num;
};
Mint::Mint() 
	: m_num(NULL)
{
}

Mint::~Mint()
{
	delete[] m_num;
}

Mint::Mint(int n)
{
	m_num = new int[n];
	int i=0;
	while(n>0)
	{
		m_num[i++] = n%10;
		n = n/10;
	}
}

Mint::Mint(const char* str)
{
	int i = 0 ;
	while(str[i] != '\0')
	{
		++i;
	}
	m_num = new int[i];
	--i;
	while(i>=0){
		m_num[i] = str[i] - '0';
		--i;
	}
}
 
Share this answer
 
Comments
Aescleal 26-Dec-15 14:54pm    
If you're going to program like it's 1993 you could at least make sure your code handles value semantics properly.
Philippe Mori 26-Dec-15 18:13pm    
Should use std::vector instead.

If you really want to use C style arrays, then at least, you should know what is a copy constructor and why you need one.
Gevorg Hai 28-Dec-15 16:43pm    
Agree about copy constructor and also assinment operator.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900