错误:请求成员“查找”在“(cstring名称)”中它是非类类型“char [2000]”
如果这很模糊,我很抱歉我对编程还很陌生(对论坛也是新手>_>)
好吧,我的代码应该从文件中读取一个数字,然后使用该数字读取该数量单词作为字典单词。然后我将这些单词存储到一个数组中并保留它们以供以后使用。在文件中的字典单词出现某个段落之后,我将其读入并将其设置为 c 字符串数组。(iv 到目前为止已全部完成)但是对于程序的最后一部分,我需要返回该段落 c - 字符串并计算每个字典单词出现的次数。我目前正在尝试 paragraph.find (word[0]) 但出现一些错误,我不知道如何修复。
错误:|40|错误:请求“段落”中的成员“查找”,其属于非类类型“char [2000]”|
代码:
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
using namespace std;
int main()
{
ifstream inStream; //declare ifstream
inStream.open("infile2.txt"); //open my file
int number; // number at the begining of the file that lets the program know
inStream >> number; // how many dictionary words are to be expected.
cout << number << " dictionary word(s)" << endl << endl;
char dict[30];
char text[2000];
char paragraph[2000]; // declareing some stuff
int count;
int position;
string word[5];
for (int i=0; i<number; i++) // using c string to set the 'number' amount of words in the dict array
{
inStream.getline(dict,30,'|');
word[i] = dict;
}
for (int i=0; i<number; i++) // cout so i can see its all set up right.
{
cout << "word " << i+1 << " is: " << word[i] << endl;
}
cout << endl;
inStream.get(paragraph,2000,'|'); // setting the rest of the paragrapg of the txt document to a c string
cout << paragraph; // so it can be searched later using the 'dict' words
position = paragraph.find (word[0]); // trying to find the position of the first word stored in 'dict[0]' but i run into an error
return 0;
}
infile2.txt 如下所示:
3steak|eggs|and|
牛排和鸡蛋,鸡蛋和牛排,鸡蛋和牛排,牛排和鸡蛋......
美味。
I'm sorry if this is vague I'm still pretty new to programming(also new to forums >_>)
Ok, my code is supposed to read in a number from a file, then use that number to read in that amount of words as dictionary words. I then store those words into an array and keep them for later usage. After the dictionary words in the file comes some paragraph, i read that in and set it to c-string array.(iv got that all down so far) But for the last part of the program i need to go back though that paragraph c-string and count how many times each dictionary word appears. I'm currently trying paragraph.find (word[0]) but i get some error that i don't know how to fix.
error: |40|error: request for member 'find' in 'paragraph', which is of non-class type 'char [2000]'|
code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
using namespace std;
int main()
{
ifstream inStream; //declare ifstream
inStream.open("infile2.txt"); //open my file
int number; // number at the begining of the file that lets the program know
inStream >> number; // how many dictionary words are to be expected.
cout << number << " dictionary word(s)" << endl << endl;
char dict[30];
char text[2000];
char paragraph[2000]; // declareing some stuff
int count;
int position;
string word[5];
for (int i=0; i<number; i++) // using c string to set the 'number' amount of words in the dict array
{
inStream.getline(dict,30,'|');
word[i] = dict;
}
for (int i=0; i<number; i++) // cout so i can see its all set up right.
{
cout << "word " << i+1 << " is: " << word[i] << endl;
}
cout << endl;
inStream.get(paragraph,2000,'|'); // setting the rest of the paragrapg of the txt document to a c string
cout << paragraph; // so it can be searched later using the 'dict' words
position = paragraph.find (word[0]); // trying to find the position of the first word stored in 'dict[0]' but i run into an error
return 0;
}
the infile2.txt looks like this:
3steak|eggs|and|
steak and eggs and eggs and steak, eggs and steak, steak and eggs...
delicious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
c-strings 不是类,并且没有 find 方法(或任何其他与此相关的方法),即
paragraph.find
。您可以尝试使用字符串,或者如果您需要使用 c 字符串,则可以使用以两个 c 字符串作为参数的 find 方法。比如这个
c-strings are not classes and do not have a find method (or any other methods for that matter) i.e
paragraph.find
. You could try using a string or if you need to use c-strings a find method that takes two c strings as parameters.such as This one