1
votes
#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

Why such error is being thrown and how to fix it?

1
Why in the world are you doing delete [] results.front();? - Blastfurnace
What is the rest of the error message (which includes the actual template parameters)? - jpalecek
You should use results.pop() to remove processed elements.... - Tony Delroy
@0x6B6F77616C74: Entirely. There's only one good place for delete[], and that's inside the destructor of a class. In this case, that destructor is std::queue<std::string>::~queue(). - MSalters

1 Answers

4
votes

Your first problem was you forgot to include <string>.

Your current problem is your delete makes no sense. Your string isn't dynamically allocated, and front() returns a reference to it anyway. 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).