我的问题是关于错误 [英] My problem is about an error

查看:69
本文介绍了我的问题是关于错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TestCase测试的主要功能中有一个错误

类型const char *的值不能使用。

什么是shuld做的! !

i没有任何sujestion。我试过白色铸造,但是没有用!!



我尝试过:



in main function in "TestCase tests" have i an error
"a value of type const char* can not be use".
what shuld be do!!
i don´t have any sujestion. i have try whit casting but , didn´t worked!!

What I have tried:

#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* extract(char* input) {
	char* ptr;
	ptr = strstr(input, "::");
	char* answer;

	if (isprint(*ptr) == 0) {
		answer = input;
	}
	else {
		while (ptr != NULL) {
			answer = ptr;
			ptr = strstr(ptr + 1, "::");

		}
		answer += 2;
	}
	printf("%s\n", answer);
	return answer;
}
// Ueberspringe alle Leerzeichen
// Rueckgabe ist Zeiger auf das erste Nichtleerzeichen
char* leerzeichen(char* input) {
	while (*input == ' ')
		input++;
	return input;
}

// Scanne durch string solange bis wir auf ein
// Leerzeichen oder das Ende des Strings treffen.
// Effektiv ueberspringen wir ein Wort.
// Rueckgabe: Zeiger auf Ende oder Leerzeichen.
char* zeichen(char* input) {
	while (*input != '\0' && *input != ' ')
		input++;
	return input;
}


int count(char* input) {
	int cnt = 0;

	// Solange das Ende nicht erreicht ist:
	// 1. Ueberspringe alle Leerzeichen
	// 2. Falls Zeichen gefunden
	//     (a) setze Zaehler hoch
	//     (b) Gehe zu Wortende

	while (*input != '\0') {
		input = leerzeichen(input);
		if (*input != '\0') {
			cnt++;
			input = zeichen(input);
		}
	}
	return cnt;
}


typedef enum {
	OK,
	FAIL
} Test;

Test testCount(char* input, int expected) {
	Test t;

	if (expected == count(input)) {
		t = OK;
	}
	else {
		t = FAIL;
	}
	return t;
}


typedef struct {
	char* input;
	int expected;
} TestCase;


void runTests(int no, TestCase test[]) {
	Test t;
	int i;

	for (i = 0; i < no; i++) {
		printf("Test %d: ", i);
		t = testCount(test[i].input, test[i].expected);
		if (OK == t)
			printf("OK \n");
		if (FAIL == t)
			printf("FAIL \n");
	}
}

#define ARRAY_SIZE 9
int main() {
	const int testNo = ARRAY_SIZE;
	TestCase tests[ARRAY_SIZE] = {
		 <big>{"", 0},
		 {"Hallo", 1},
		 {"  Hallo", 1},
		 {"Hallo", 1},
		 {"  Hallo  ", 1},

		 {"Hal lo", 2},
		 {" Hal lo", 2},
		 {"Hal lo ", 2},
		 {" Hal lo  ", 2}
	};</big>	runTests(testNo, tests);

}

推荐答案

尝试更改 TestCase 如下:

Try changing the defiintion of TestCase as follows:
typedef struct {
	const char* input;
	int expected;
} TestCase;


我将您的代码复制并粘贴到在线C ++系统中,它可以工作:https://www.onlinegdb.com/online_c++_compiler [ ^ ]



编译干净,运行,我得到:

I copy and paste your code into an online C++ system, and it works: https://www.onlinegdb.com/online_c++_compiler[^]

It compiles cleanly, it runs, and I get:
Test 0: OK 
Test 1: OK 
Test 2: OK 
Test 3: OK 
Test 4: OK 
Test 5: OK 
Test 6: OK 
Test 7: OK 
Test 8: OK



所以......我们需要知道什么编译器,什么行等等。



或者,更改你的TestCase结构以接受const字符串:


So ... we would need to know what compiler, what line, and so forth.

Alternatively, change your TestCase struct to accept const strings:

typedef struct {
	const char* input;
	int expected;
} TestCase;

你必须将它与你所有的函数调用匹配,否则他们就赢了;编译它们!

You'll have to match that with all your function calls though or they won;t compile either!


这篇关于我的问题是关于错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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