我如何解决strtok无法strtok':无法将参数1从'char'转换为'char *' [英] how do i solve strtok cannot strtok' : cannot convert parameter 1 from 'char' to 'char *'

查看:188
本文介绍了我如何解决strtok无法strtok':无法将参数1从'char'转换为'char *'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,看我的代码我有3个错误,我该如何解决?请帮帮我.

Hey friends, look my code I have 3 errors, how can I solve this? Please help me.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
class converter
	{
	char co;
public:
	conver(char co1)
		{
		co=co1;
		int s,s1,s2;
		char *p;
		p=strtok(co,".");
		if(p)
			{
			s=atoi(p)*12;
		}
		p=strtok(NULL,".");
		if(p)
			{
			s1=atoi(p);
		}
		s2=s+s1;
		return s2;
	}
};
class rectangle
	{
	char i,j,sqft,mul;
public:
	rec(char p,char q)
	{i=p;j=q;}
	char show_area();
};

char rectangle::show_area()
	{
	float a;
	float mul=0;
	converter ob1;
	a=ob1.conver(i)*ob1.conver(j);
	mul=a/144;
	return mul;
}
class triangle
	{
	char i1,j1;
public:
	tri(char p1,char q1)
		{
		i1=p1;
		j1=q1;
	}
	char show_area1();
};
char triangle::show_area1()
	{
	float a1,mul1=0;
	converter ob2;
	a1=ob2.conver(i1)*ob2.conver(j1)*(0.5);
	mul1=a1/144;
	return mul1;

}
class area:public rectangle,public triangle
	{
	char length,width,height;
public:
	area(char length1,char width1,char height1):rec(length1,width1),tri(length1,width1)
		{
length=length1;
width=width1;
height=height1

	}

void show()
	{
	mout<<"rectangl's area is:"<<show_area();
	mout<<"triangle's area is:"<<show_area1();
}



};
class volume :public area
	{
	char h,l,w;
public:
	volume(char lenght,char width,char height)
		{
		l=legth;
		w=width;
		h=height;
	}
	


void show_area2()
	{
	float a3,mul3=0;
	converter ob3;
	a3=ob3.conevr(l)*ob3.conver(w)*ob3.conver(h);
	mul3=a3/144;
	mout<<"Volume is:"<<a3;
}


};
int main()
	{
	char a[10],b[10],c[10];
	ifstream mit("inp.txt");
	ofstream mot("oup.txt");
	mit>>a>>b>>c;
	area ob(a,b,c);
	ob.show();
	volume obv(a,b,c);
	return 0;
}



但随后显示了3个错误:

1.C:\ TC \ BIN \ mitu \ assignment11.cpp(15):错误C2664:``strtok'':无法将参数1从``char''转换为``char *''
从整数类型到指针类型的转换需要reinterpret_cast,C样式强制转换或函数样式强制转换

2.:\TC\BIN\mitu\assignment11.cpp(118):错误C2664:"__ thiscall area :: area(char,char,char)":无法从"char [10]"转换参数1到``char''
这种转换需要reinterpret_cast,C样式的强制转换或函数样式的强制转换

3.:\TC\BIN\mitu\assignment11.cpp(120):错误C2664:"__ thiscall volume :: volume(char,char,char)":无法从"char [10]"转换参数1到``char''
这种转换需要reinterpret_cast,C样式的强制转换或函数样式的强制转换
帮助我的朋友,我真的需要帮助.



But then it shows 3 errors:

1.C:\TC\BIN\mitu\assignment11.cpp(15) : error C2664: ''strtok'' : cannot convert parameter 1 from ''char'' to ''char *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

2.:\TC\BIN\mitu\assignment11.cpp(118) : error C2664: ''__thiscall area::area(char,char,char)'' : cannot convert parameter 1 from ''char [10]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

3.:\TC\BIN\mitu\assignment11.cpp(120) : error C2664: ''__thiscall volume::volume(char,char,char)'' : cannot convert parameter 1 from ''char [10]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
help me friends i really need help.

推荐答案

您需要考虑要尝试做的事情:
You need to think about what you are trying to do: strtok[^] takes a char* parameter not a char, just as the error explains.

char co;
...
p=strtok(co,".");

因此,与其尝试一次传递一个字符,不如将一串字符传递给它?

So rather than trying to hand a single character at a time, why not pass it a string of them?


1.在p=strtok(co,".");中,cochar而不是char*.
应该与两个char*参数一起使用.
1. In p=strtok(co,".");, co is char and not char*.
It is supposed to be used with two char* parameters.
char * strtok ( char * str, const char * delimiters );



对于2和3,您传递的是char[],其中必须使用char作为参数.



For 2 and 3, you are passing char[] where char is required as parameter.


1.甚至TC也随附帮助文件,这些文件指示了各种功能的使用.

2. char *不等于char

3.您应该将变量与_有意义的名称_一起使用,如果这样做,则有可能猜测您希望使用转换器类实现的目标.

4.未定义converter :: conver的返回类型.

5.将一个字节传递给conver成员函数,但将其视为字符串.


如果您要声明要实现的目标,那将是一个谨慎的举动.
据我所知,您应该删除对转换器"类的所有引用.
您似乎在将二进制值假装为字符串并将这些字符串转换为数字表示形式之前将其传递给"conver". wtf?

以以下代码段为例:
1. Even TC comes with help files that indicate the use of various functions.

2. char* does not equal char

3. You should use variable with _meaningful names_, had this be done it may be possible to guess what you hoped to achieve with the converter class.

4. The return type of converter::conver is not defined.

5. You pass a single byte to the conver member-function, yet treat it as though it was a string.


It would be a prudent move if you would state what you are trying to achieve.
From what I can tell, you should delete all references to the class "converter".
You appear to be passing binary values to "conver" before pretending that they are strings and proceeding to convert these strings to a numerical representation. wtf?

Take for example this snippet:
void show_area2()
	{
	float a3,mul3=0;
	converter ob3;
	a3=ob3.conevr(l)*ob3.conver(w)*ob3.conver(h);
	mul3=a3/144;
	mout<<"Volume is:"<<a3;
}



忽略144的转换因子(这对结果没有影响),可以很简单地将其重写为:



Disregarding the conversion factor of 144(which bears no impact on the result), this would be quite simply rewritten as:

void show_area2()
{
	mout<<"Volume is:"<< (l *  w * h);
}


这篇关于我如何解决strtok无法strtok':无法将参数1从'char'转换为'char *'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆