【问题标题】:Pointer/String arrays though functions c++指针/字符串数组虽然函数c ++
【发布时间】:2019-02-27 01:24:50
【问题描述】:

我有这个程序,我无法开始工作。假设制作一个用户制作的字典,然后将其打印出来(还没有解决那个部分)当我尝试运行它时,我得到一个 wntdll.pbd not loaded 错误的东西弹出(见截图)没有其他错误或任何东西。

#include <iostream>
#include <string>

using namespace std;

int numItems;

void userInput(string *terms, string *def) {

    for (int i = 0; i < numItems; ++i) {
        cout << "Enter Term: ";
        cin >> terms[i];
        cout << endl;

        cout << "Enter Def for that term: ";
        cin >> def[i];
        cout << endl;

    }
}


void outputdef() {

}

int main() {

    cout << "How many Items do you want: ";
    cin >> numItems;
    cout << endl;

    string *ptrTerms = new string[numItems];
    string *ptrDef = new string[numItems];

    userInput(ptrTerms, ptrDef);

    delete ptrTerms;
    delete ptrDef;

}

不知道是文件问题还是代码问题。感谢您的帮助。

编辑:即使按照其他帖子的说明进行操作后,它仍然会抛出错误。它显示的错误在 delete_scalar.cpp 中。

【问题讨论】:

  • 不要使用new[],使用std::vector&lt;std::string&gt;。至于您的代码:newdelete 一起使用,new[]delete[] 一起使用。
  • @Swordfish -- 有时使用new/delete 是课程的重点。但是,如果做不到这一点,我同意。 (而且由于代码使用string,显然不利于容器使用)。
  • @DavidC.Rankin 如果这是从该作业中吸取的教训,那就太愚蠢了。
  • 不,一点也不。许多现有的代码库使用new/delete。不教它并让一代程序员去维护他们没有背景的代码会更具破坏性。从 80 年代后期开始编写的大量 C++ 代码仍然潜伏着。

标签: c++


【解决方案1】:

正如@Swordfish 指出的那样,它是delete[]。这解决了问题。谢谢。也是为了上课,是的,这很愚蠢,因为我会使用矢量但我们不能。

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2018-05-08
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2012-03-10
    • 2012-02-08
    相关资源
    最近更新 更多