从'const char *'到'int'的无效转换 [英] invalid conversion from 'const char*' to 'int'
问题描述
#include < iostream >
#include < string < span class =code-keyword>>
使用 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(字符串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; // 重要的是我以这种方式初始化
}
当我尝试编译时我得到一个错误
无效转换从'const char *'到'int'Mint a =065272005572
并且
初始化'Mint :: Mint(int)'的参数1 [-fpermissive]
Mint(int);
任何帮助;
你的代码有三个直接问题。
第一个(和编译器有的那个)一个抱怨的是,它无法解决你想要调用的构造函数。当你给它一个int
它可以解决你想要的第一个被调用。但是当你给它一个const char *
时,它不知道你想要调用的字符串
构造函数。所以如果你想要调用的字符串构造函数实际给它一个字符串
!
Mint b {std :: string { 1234567}};
是你需要使用的东西,如果你有一个现代的编译器
< pre lang =c ++> Mint b(std :: string( 1234567));
如果你还没有。
第二个问题是你怎么样'重新存储你的数字。比使用内置数组更好的方法是使用std :: vector
。它可以动态调整大小,一旦调整大小,通常和内置数组一样快。
最后一个问题是处理对象的复制和分配。考虑当你做以下事情时会发生什么:
Mint a = 137;< br />
Mint b = a; < br />
希望如果你使用向量
来存储你的数字这个问题解决了,但如果你自己尝试管理内存,那么你应该考虑这个问题。
/ / #include< iostream>
// #include< string>
// 使用命名空间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;
}
}
#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;
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 anint
it can work out you want the first called. However when you give it aconst char *
it has no idea that you want thestring
constructor called. So if you want the string constructor called actually give it astring
!
Mint b{ std::string{ "1234567" } };
is the sort of thing you need to use if you've got a modern compiler
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 astd::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 avector
to store your digits this problem solves itself but if you try and manage memory yourself then it's something you should consider.
//#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; } }
这篇关于从'const char *'到'int'的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!