为什么我得到 <function> is not member of <class>?我试图从中传递一个字符串和 return
Why am I getting a <function> is not a member of <class>? I was trying to pass a string and return from it
我正在尝试将一个字符串传递给一个函数,该函数将被排序。然后让排序后的字符串成为 return 。它不会编译,它甚至说 "binarySort is not a member of Others".
是不是不允许的事情?
这是 Class 文件
#include "Others.h"
#include <iostream>
using namespace std;
char Others::inputCheck(char guess) {
while (!isalpha(guess)) { //If the inputs are not alphabets, keep looping.
cout << "Enter again: ";
cin >> guess;
}
cin.ignore(1, '\n');
guess = tolower(guess);
return guess;
}
string Others::binarySort(string sampleText) {
//Bubble sorting the string. (Copy pasted)
for (int i = 0; i < sampleText.length(); i++)
{
for (int j = i + 1; j < sampleText.length(); j++)
{
if (sampleText[i] > sampleText[j]) //if previous has bigger ascii value than next,
{
//swapping the prev and next characters
char temp = sampleText[i];
sampleText[i] = sampleText[j];
sampleText[j] = temp;
}
}
}
return sampleText;
}
这是头文件。
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
class Others
{
public:
char inputCheck(char guess); //Function to check if inputs are alphabets.
string binarySort(string sampleText);
};
#endif
主要功能。
#include <iostream>
#include "Others.h"
using namespace std;
int main() {
string sortedText, sampleText = "toibeawn";
Others sorter;
sortedText = sorter.binarySort(sampleText);
cout << "Text after sorted:\n";
cout << sortedText;
return 0;
}
如果不用于函数,则排序有效。
错误输出:
1>Sorting_test.cpp
1>Others.cpp
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,7): 错误 C2039: 'string': 不是 'std'
的成员
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ 类型(编译器内部)(368):消息:参见 'std'
的声明
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24):错误 C3646:'binarySort':未知覆盖说明符
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C2059: 语法错误: '('
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,30): error C2039: 'string': 不是 'std'
的成员
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ 类型(编译器内部)(368):消息:参见 'std'
的声明
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,48):错误 C2238:“;”之前的意外标记
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.cpp(16,21): 错误 C2039: 'binarySort': 不是 'Others'
的成员
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(5): 消息:参见 'Others' 的声明
1>正在生成代码...
1> 完成构建项目 "Sorting_test.vcxproj" -- 失败。
您在声明 using namespace std
之前包含 "Others.h",这导致 string
在包含的 header 中无法识别。
在header"Others.h"中添加using namespace std;
,错误就没有了。
通常,在 header 中使用 using namespace std
是一种不好的做法,您最好只写 std::string
.
std::string binarySort(std::string sampleText)
需要命名空间
一切都很好。需要更改一个简单的更改。
- 在您的 header 中包含此 header :#include 并使用 std 命名空间。
所以在更改之后,您的 header 看起来像
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
#include <iostream>
using namespace std;
主要问题是,在您的 header 中,由于缺少字符串 header 和 std 命名空间,字符串未解析。
注意:始终尝试先包含语言 header,然后再包含您的自定义 header。
我正在尝试将一个字符串传递给一个函数,该函数将被排序。然后让排序后的字符串成为 return 。它不会编译,它甚至说 "binarySort is not a member of Others".
是不是不允许的事情?
这是 Class 文件
#include "Others.h"
#include <iostream>
using namespace std;
char Others::inputCheck(char guess) {
while (!isalpha(guess)) { //If the inputs are not alphabets, keep looping.
cout << "Enter again: ";
cin >> guess;
}
cin.ignore(1, '\n');
guess = tolower(guess);
return guess;
}
string Others::binarySort(string sampleText) {
//Bubble sorting the string. (Copy pasted)
for (int i = 0; i < sampleText.length(); i++)
{
for (int j = i + 1; j < sampleText.length(); j++)
{
if (sampleText[i] > sampleText[j]) //if previous has bigger ascii value than next,
{
//swapping the prev and next characters
char temp = sampleText[i];
sampleText[i] = sampleText[j];
sampleText[j] = temp;
}
}
}
return sampleText;
}
这是头文件。
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
class Others
{
public:
char inputCheck(char guess); //Function to check if inputs are alphabets.
string binarySort(string sampleText);
};
#endif
主要功能。
#include <iostream>
#include "Others.h"
using namespace std;
int main() {
string sortedText, sampleText = "toibeawn";
Others sorter;
sortedText = sorter.binarySort(sampleText);
cout << "Text after sorted:\n";
cout << sortedText;
return 0;
}
如果不用于函数,则排序有效。
错误输出:
1>Sorting_test.cpp 1>Others.cpp 1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,7): 错误 C2039: 'string': 不是 'std'
的成员1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ 类型(编译器内部)(368):消息:参见 'std'
的声明1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24):错误 C3646:'binarySort':未知覆盖说明符
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C2059: 语法错误: '(' 1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,30): error C2039: 'string': 不是 'std'
的成员1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ 类型(编译器内部)(368):消息:参见 'std'
的声明1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,48):错误 C2238:“;”之前的意外标记
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.cpp(16,21): 错误 C2039: 'binarySort': 不是 'Others'
的成员1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(5): 消息:参见 'Others' 的声明 1>正在生成代码... 1> 完成构建项目 "Sorting_test.vcxproj" -- 失败。
您在声明 using namespace std
之前包含 "Others.h",这导致 string
在包含的 header 中无法识别。
在header"Others.h"中添加using namespace std;
,错误就没有了。
通常,在 header 中使用 using namespace std
是一种不好的做法,您最好只写 std::string
.
std::string binarySort(std::string sampleText)
需要命名空间
一切都很好。需要更改一个简单的更改。
- 在您的 header 中包含此 header :#include 并使用 std 命名空间。
所以在更改之后,您的 header 看起来像
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
#include <iostream>
using namespace std;
主要问题是,在您的 header 中,由于缺少字符串 header 和 std 命名空间,字符串未解析。
注意:始终尝试先包含语言 header,然后再包含您的自定义 header。