我有下一个代码:
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <tchar.h>
class TList {
private:
std::vector<const char*> elementos;
int position;
TList(std::vector<const char*> elementos);
public:
TList(const char** e, int s);
TList *GetReverseList();
int Size();
const char *Next();
};
TList::TList(const char** e, int s) {
std::vector<const char*> res (&e[0], &e[s]);
elementos = res;
position = 0;
}
TList::TList(std::vector<const char *> elements) {
std::vector<const char*> res = std::vector<const char*>();
int size = elements.size();
for (int i = 0; i < size; i++) {
res.push_back(elements.at(i));
}
elementos = res;
position = 0;
}
//Create a new TList with the reverse list of elements
TList *TList::GetReverseList() {
TList *res = new TList(elementos);
std::reverse(res->elementos.begin(), res->elementos.end());
return res;
}
int TList::Size() {
return elementos.size();
}
//Use the position to get the next char *
const char * TList::Next() {
const char * res;
if (elementos.empty()) {
res = NULL;
}
else {
int pos = position;
int size = elementos.size();
res = pos == size ? elementos.at(position - 1) : elementos.at(position);
if (pos < size) {
position++;
}
}
return res;
}
int main()
{
int size = 2;
const char *arr[2] = {"Hola", "AAAA"};
TList *list = new TList(arr, size);
TList *listReverse = list->GetReverseList();
printf("Size: %u \n", listReverse->Size());
printf("First value: %s \n", listReverse->Next());
printf("Second value: %s \n", listReverse->Next());
delete list;
delete listReverse;
return 0;
}
当我在Visual Studio中运行它时,它会在控制台中显示
Size: 2
First Value: AAAA
Second Value: Hola
并引发异常“未加载ucrtbased.pdb”“ ucrtbased.pdb包含查找模块ucrtbased.dll的源所需的调试信息”,这是一个图像:
并打开此窗口:
尝试删除listReverse时会抛出所有这些异常,这是调用堆栈的另一个捕获(忽略诸如listCopy之类的其他变量):
我想创建一个尽可能优化的新TList,但要反转其元素,我不想做一个复制构造函数,因为我需要将它作为函数“ GetReverseList”使用,所以我该怎么办?
答案 0 :(得分:0)
在elements
的{{1}}中,您不应使用TList::TList(std::vector<const char *> elements)
作为参数,而已将elements
定义为类成员。
跟随code
可以起作用:
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <iostream>
//#include <tchar.h>
class TList {
private:
std::vector<const char*> elementos;
int position;
TList(std::vector<const char*> elementos);
public:
TList(const char** e, int s);
TList *GetReverseList();
int Size();
const char *Next();
};
TList::TList(const char** e, int s) {
std::vector<const char*> res (&e[0], &e[s]);
elementos = res;
position = 0;
}
TList::TList(std::vector<const char *> ve) {
std::vector<const char*> res = std::vector<const char*>();
int size = ve.size();
for (int i = 0; i < size; i++) {
res.push_back(ve.at(i));
}
elementos = res;
position = 0;
}
//Create a new TList with the reverse list of elements
TList *TList::GetReverseList() {
TList *res = new TList(elementos);
std::reverse(res->elementos.begin(), res->elementos.end());
return res;
}
int TList::Size() {
return elementos.size();
}
//Use the position to get the next char *
const char * TList::Next() {
const char * res;
if (elementos.empty()) {
res = NULL;
}
else {
int pos = position;
int size = elementos.size();
res = pos == size ? elementos.at(position - 1) : elementos.at(position);
if (pos < size) {
position++;
}
}
return res;
}
int main()
{
int size = 2;
const char *arr[2] = {"Hola", "AAAA"};
TList *list = new TList(arr, size);
TList *listReverse = list->GetReverseList();
printf("Size: %u \n", listReverse->Size());
printf("First value: %s \n", listReverse->Next());
printf("Second value: %s \n", listReverse->Next());
delete list;
delete listReverse;
return 0;
}
答案 1 :(得分:0)
我意识到错误是不同的,因为正如我之前评论的那样,主要问题是代码的“ listReverse”是“ listReverseOptimizada”,解析中的其他“ listReverse”是“ TList”而不是“ TList *”因此,当我尝试删除它时,我将“ delete&listReverse”放到了它并引发错误,如何删除“ TList”对象?
这是包含该元素的完整代码:
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <tchar.h>
class TList {
private:
std::vector<const char*> elementos;
int position;
TList(std::vector<const char*> elementos);
public:
TList(const char** e, int s);
TList(const TList & list);
TList GetReverseList(TList lstSrc);
TList *GetReverseList();
int Size();
const char *Next();
};
TList::TList(const char** e, int s) {
std::vector<const char*> res (&e[0], &e[s]);
elementos = res;
position = 0;
}
TList::TList(std::vector<const char *> elements) {
std::vector<const char*> res = std::vector<const char*>();
int size = elements.size();
for (int i = 0; i < size; i++) {
res.push_back(elements.at(i));
}
elementos = res;
position = 0;
}
TList::TList(const TList & list) {
std::vector<const char*> el = std::vector<const char*>();
int size = list.elementos.size();
for (int i = 0; i < size; i++) {
el.push_back(list.elementos[i]);
}
elementos = el;
position = 0;
}
//Create a new TList * with the reverse list of elements
TList *TList::GetReverseList() {
TList *res = new TList(elementos);
std::reverse(res->elementos.begin(), res->elementos.end());
return res;
}
//Create a new TList with the reverse list of elements
TList TList::GetReverseList(TList lstSrc) {
TList *res = new TList(lstSrc);
std::reverse(res->elementos.begin(), res->elementos.end());
return *res;
}
int TList::Size() {
return elementos.size();
}
//Use the position to get the next char *
const char * TList::Next() {
const char * res;
if (elementos.empty()) {
res = NULL;
}
else {
int pos = position;
int size = elementos.size();
res = pos == size ? elementos.at(position - 1) : elementos.at(position);
if (pos < size) {
position++;
}
}
return res;
}
int main()
{
int size = 2;
const char *arr[2] = {"Hola", "AAAA"};
TList *list = new TList(arr, size);
TList listReverse = list->GetReverseList(*list);
TList *listReverseOptimized = list->GetReverseList();
delete list;
delete listReverse;
delete listReverseOptimized;
return 0;
}
我尝试放置“ delete listReverse”而不是“ delete&listReverse”,但是它不起作用,这是一张图片: https://jwt.io