简体   繁体   English

Stl容器的std :: basic_string类型

[英]Stl container of std::basic_string type

#ifndef UNICODE
#define UNICODE
#endif

#include <iostream>
#include <queue>
#include <stdio.h>
#include <Windows.h>
#include <string>
using namespace std;

int __cdecl main()
{   
    std::queue<std::basic_string<TCHAR>> results;

    results.push(TEXT("Hello world! ♥☻☺"));

    wcout<<results.front();
    delete [] results.front();

    system("pause");
    return 0;
}

Error 1 error C2440: 'delete' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'void *' C:\\Users\\Tomek\\Documents\\Visual Studio 2010\\Solutions\\clean_rough_draft\\clean_rough_draft\\main.cpp 20 1 clean_rough_draft 错误1错误C2440:'delete':无法从'std :: basic_string <_Elem,_Traits,_Ax>'转换为'void *'C:\\ Users \\ Tomek \\ Documents \\ Visual Studio 2010 \\ Solutions \\ clean_rough_draft \\ clean_rough_draft \\ main .cpp 20 1 clean_rough_draft

Why such error is being thrown and how to fix it? 为什么会抛出这样的错误以及如何修复它?

Your first problem was you forgot to include <string> . 你的第一个问题是你忘了包含<string>

Your current problem is your delete makes no sense. 你当前的问题是你的删除毫无意义。 Your string isn't dynamically allocated, and front() returns a reference to it anyway. 您的字符串未动态分配,而front()仍会返回对它的引用。 So, you're trying to call array delete on something that isn't an array (a string is an object that encapsulates an array) and that wasn't dynamically allocated in the first place (and on a reference instead of a pointer). 所以,你试图在不是数组的东西上调用数组删除(字符串是封装数组的对象)并且首先没有动态分配(并且在引用而不是指针上) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM