【问题标题】:C Vector error 'char(*)[]: unknown size' - vector erase iterator outside rangeC向量错误'char(*)[]:未知大小'-向量擦除迭代器超出范围
【发布时间】:2018-01-27 21:23:20
【问题描述】:

很抱歉我是 C 新手。我之前学过一点 C++,可能会混淆 C 和 C++。但我正在尝试编写一个 C 程序。我唯一可以在 C++ 中应用的是向量。这对我来说是一个练习。请告诉我我犯的任何错误。非常感谢。

我正在开发一个程序来计算文本文件中单词的频率,并输出文本文件中的单词列表和相应的频率。程序中止并显示错误消息:

C2036: 'char(*)[]: unknown size'.

它指向矢量文件中的以下几行:

1475:   _Move_unchecked(_VIPTR(_Where) + 1, this->_Mylast(), _VIPTR(_Where));

1476:   _Destroy(this->_Mylast() - 1, this->_Mylast());

1478:   --this->_Mylast();

我能理解的是在

line 1474: _DEBUG_ERROR("vector erase iterator outside range");

但我不知道这个问题。我确实搜索了类似的问题,但我发现解决方案是针对海报代码的。请帮忙。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <ctype.h>


void main(int argc, char *argv[])
{
char *str;
int wordLocation, insertLocation, wordLength;
wordLocation = insertLocation = wordLength = 0;
char *wordCheck;

std::vector<char[]> word;
std::vector<int> frequency;
std::vector<char[]> tempWord;

int tempFreq;

FILE *file;
file = fopen(argv[1], "r");

if (file)
{
    //Read file test
    /*while (fscanf(file, "%s", str)!= EOF)
    {
        printf("s%\n",str);
    }
    */

    while (fscanf(file, "%s",word.end()) != EOF) 
    {
        wordCheck = word[wordLocation];
        wordLength = strlen(wordCheck);

        for (int i = 0; i < wordLength; i++, wordCheck++)
        {
            // Check and remove punctuation
            if (ispunct(*wordCheck))
            {
                *wordCheck = ' ';
            }

            // Check uppercase and transform to lowercase
            if (*wordCheck >= 'A' && *wordCheck <= 'Z')
            {
                *wordCheck = putchar(tolower(*wordCheck));
            }
        }

        // Start to compare word in vector word
        if (int(word.size()) == 1) 
        {
            //only contain 1 input
            frequency[wordLocation] = 1;
        }
        else
        {
            for (int i = 0; i <= wordLocation; i++)
            {
                // Look for the same word 
                if (word[i] == word[wordLocation])
                {
                    frequency[i]++;
                    word.erase(word.end()-1);
                    break;
                }
                // Add new word at the end
                else if (i == wordLocation && word[i] != word[wordLocation])
                {
                    frequency[wordLocation] = 1;
                }
            }
        }
        wordLocation++;
    }
    // Word Sorting
    for (int i = 0; i <= wordLocation; i++) 
    {
        for (int k = 0; k <= wordLocation - 1; k++)
        {
            if (frequency[i] < frequency[i + 1])
            {
                //Swap element

                tempWord.insert(tempWord.begin(),word[i]);
                tempFreq = frequency[i];

                word.erase(word.begin());
                frequency.erase(frequency.begin());
                word.insert(word.begin() + i + 1, tempWord.front());
                frequency.insert(frequency.begin() + i + 1,tempFreq);

                tempWord.pop_back();
            }
        }
    }
    // After sorting, write into the file
    fclose(file);

    remove("output.txt");

    file = fopen("output.txt", "a");
    file = fopen("output.txt", "w");

    for (int i = 0; i <= wordLocation; i++)
    {
        fprintf(file, "%s %d \n", word[i], frequency[i]);
    }
    printf("Create output.txt successed.");
}
else
{
    printf("Read file fail. Please check the input path.\n");
}

}

【问题讨论】:

  • 提炼你的程序,直到它最小但仍然崩溃。
  • 这不是 C 而是 C++,不同的语言。
  • Jonathan Leffler 注意到 Stack Overflow 主动 建议同时标记两种语言He posted to meta about it.
  • 如果你想写一个 C 程序,你的第一个错误是你写了一个 C++ 程序。那么,您可能应该删除 C++ 标记。
  • 最好使用C编译器来编写“C程序”。

标签: c++ c arrays pointers vector


【解决方案1】:

C2036: 'char(*)[]: 未知大小'。

这是因为

std::vector<char[]> tempWord;

错误(您必须指定大小或使用指针)以及在C++ 中使用char 数组是个坏主意。

请改用 std::stringstd::array

std::vector<std::string> tempWord;

void main 导致未定义的行为

入口点必须声明为

int main(void)
{
    return 0; // Optimal
}

或带参数

int main(int argc, char *argv[])
{
    return 0; // Optimal
}

此外,您正在使用未初始化的指针并尝试将数据复制到那里 -> UB

char *str;
while (fscanf(file, "%s",str) != EOF)

我建议你使用自动数组,因为你是初学者。

char str[1024];

第 1474 行:_DEBUG_ERROR("向量擦除迭代器超出范围");

您正在索引一个 std::vector,而它还没有任何推送值 -> UB

word[wordLocation];

首先你必须将一些字符串推入容器中

std::string userInput;
std::cin >> userInput;        // User will enter some string
word.push_back(userInput);    // Push that input into the vector

【讨论】:

  • 感谢您的回复。很抱歉我是 C 新手。我之前学过一点 C++,我可能会混淆 C 和 C++。但我正在尝试编写一个 C 程序。我唯一可以在 C++ 中应用的是向量。这对我来说是一个练习。请告诉我我犯的任何错误。非常感谢。
  • 每个人都曾经是初学者,犯过和你一样的错误:-)我建议你分部分做,不要写一个400行的源代码然后调试它,先尝试运行从文件中扫描,如果有,则进行下一步操作,依此类推
  • @FilipKočica: 呃,好吧,不是真的...... - 当我学习 C 时,没有 C++...... ;-)
  • 我的意思是比喻性的:-)
  • @FilipKočica 是的,我一步一步做到了。实际上代码已成功构建,但无法运行。所以我尝试调试。我在修改中犯了一些错误,所以我遇到了这种情况:'(。我想我误导了练习问题:允许使用 std::vector,但没有“使用命名空间 std”。如果这不再是 C 程序了吗我有什么要使用的吗?谢谢大家。
【解决方案2】:

C 和 C++ 是不同的语言。您不能编写 C 程序并在其中使用一点 C++。

C 中没有std::vector。如果要使用std::vector,则必须用C++ 编写。如果你想用C写,你不能使用std::vector

不要使用 C++ 编译器来编译 C 程序。这只会增加你的困惑。

【讨论】:

  • 感谢您的提醒。使用 std::vector 是问题的建议。如果我想在 C 中存储“char 数组”列表,您有什么建议存储它吗?但是整个“字符串”的大小取决于用户从文本文件中输入的内容。我考虑过使用 struct 但我怀疑输入的可变大小可能会在某些特定情况下导致程序崩溃。
  • 如果你的练习提到了using namespace std,并且他们明确允许std::vector,他们可能是在教你C++,而不是C。如果有疑问,请询问你的助教。
  • 他们不允许“使用命名空间std”,除了“std::vector”
  • 看,我已经尽力解释它,但可能还不够努力。在我放弃之前让我再试一次。 如果他们说什么 --- 什么 --- 关于允许或不允许 using namespace stdstd::vector,很可能,您正在参加 C++ 课程。如有疑问,请咨询您的助教。
  • 知道了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
相关资源
最近更新 更多