I've got an issue right here, I'm getting a memory violation error (I am handling the memory) and also a wrong input. What I am doing is a pointer managed list.
My code is supposed to do: Update the referenced pointer with multiple entries and print them. It does it partially, let me show you.
Code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
struct Lyric
{
Lyric* next;
tuple<int, string> tuple;
};
void Addition(Lyric*& poetry, tuple<int, string> tpl)
{
Lyric* newl = new Lyric;
newl->tuple = tpl;
newl->next = poetry;
poetry = newl;
}
void PrintScr(Lyric*& poetry)
{
if (poetry == NULL)
{
cout << "Empty list !" << endl;
return;
}
else
{
Lyric* prel = poetry;
while (prel != NULL)
{
cout << "Printing the integer: " << get<0>(prel->tuple) << endl;
cout << "Printing the string : " << get<1>(prel->tuple) << endl;
cout << "------------------------------------------" << endl;
prel = prel->next;
}
}
}
void main()
{
string a_str[] = {"test1", "test2"};
Lyric* poetry = new Lyric();
/*
int size = 1;
for (int i = 0; i <= size; i++)
Addition(poetry, i, make_tuple(i, a_str[i]));
*/
Addition(poetry, make_tuple(0, a_str[0]));
Addition(poetry, make_tuple(1, a_str[1]));
PrintScr(poetry);
system("PAUSE");
}
Output:
So it's supposed to print them in the order they've been added . My best bet is that I've screwed something up in the PrintScr method because it prints them in reverse and also prints a non-existing item, but I am not sure what I am doing wrong, I am going through the elements one by one and printing them....
This is how it's supposed to look like :
Printing the integer : 1
Printing the string : test1
-------------------------------
Printing the integer : 2
Printing the string : test2
-------------------------------
Aucun commentaire:
Enregistrer un commentaire