A fairly difficult problem to search for...

So basically I'm making a generic sort of class that takes in something and can store, compare it, etc. For testing I just set it up to take in a char and (ideally) print one back out, but whenever I do, it just prints out a bunch of Hex at me. I've shrunk it down for easy of reading.

The header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef GENERIC_H
#define GENERIC_H

#include <iostream>
using namespace std;

class Generic {
    friend ostream& operator<<(ostream &, const Generic &);

public:
    Generic (char);

private:
    char *charPtr;
    int size;
};

#endif 


The .cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Generic.h"

Generic::Generic(char c){ //size is there for future use
    size = 1;
    charPtr = new char[size];
    charPtr[0] = c;
}

ostream& operator<<(ostream &output, const Generic &toPrint){
    output << toPrint.charPtr[0];
    return output;
}


The driver I've been using just reads:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Generic.h"
using namespace std;

int main(){
    Generic *toTest = new Generic('a');
    cout << toTest << endl;
    return 0;
}


And my output is 8 hex digits, the first four of which vary each time I run it and the last four are 7640 (which I'm guessing is related to 'a' somehow)

Any ideas? This is part of a giant sized project, I've just narrowed the issue to here and tried to generalize the question as much as I can.

Cheers!

Small edit: The reason for not simply saying "Generic toTest(a)" is that the driver I'm testing for specifically creates a new Generic pointer (Fun stuff, I know).
Last edited on
It should be cout << *totest << endl;.
Wow, maybe by senior year I'll remember to dereference. *smh* Thanks for the quick reply, I'm as grateful as I am embarrassed. Just saved me a lot of pain :)
Topic archived. No new replies allowed.