I am trying to initialize an object by passing it a string, and initialzing it to a member variable called 'name'. However, when I try to access name through the pointer to the object, there is nothing there. Do I need to use pointers and references to achieve this?
1 2 3 4 5 6 7 8 9 10 11
// main.cpp
#include "carouselItem.h"
//========================================================================
int main( ){
string one = "One";
CarouselItem *fp1 = new CarouselItem(one);
cout << "Name: " << fp1->name << endl;
}
The problem is probably with this: name=name;
Give the argument a different name.
(And it is recommended to use the initialization list syntax in this scenario.)