I am new to C++ and working on a project where I have array of instances of a class, and within that class I have a struct that within that I have a function. How do I use that function within my main code block. I tried
class artwork {
struct art {
// the struct art contains important information such as artist, title and medium.
private:
string artist;
public:
void setArtist(string values);
string getArtist();
};
void artwork::art::setArtist(string values) {
artist = values;
}
int main (){
artwork myartwork[500];
for (int i = 0; i < 500; i++) {
///-----------------------------------
///-----------------------------------
// below is where the error occurs? How do I reference setArtist?
///-----------------------------------
///-----------------------------------
cout << myartwork[0].art.setArtist("Tim");
}
system("pause");
return 0;
}
Several things need to change here:
class artwork
needs a member variable of type art
art
, because that already names the type of the structartwork
Your code should probably resolve to something like this:
class artwork {
public:
struct art {
private:
string artist;
public:
void setArtist(string values){artist = values;}
string getArtist(){return artist;}
} art_;
};
int main (){
artwork myartwork[500];
for(int i = 0; i < 500; i++) {
myartwork[i].art_.setArtist("Tim");
cout << myartwork[i].art_.getArtist();
}
}
Other helpful tips:
EDIT:
Think of your class definition as the blueprint and the member as the actual object. If we wanted an artwork
that had multiple art
objects, in the case of a collaboration perhaps, then we could do this:
class artwork {
public:
struct art {
private:
string artist;
public:
void setArtist(string values){artist = values;}
string getArtist(){return artist;}
};
art art1_;
art art2_;
};
Note that we don't define the struct
twice, we just define 2 objects. Then if we had an artwork object like: artwork foo
we could do:
foo.art1_("Sam Arnold"s);
foo.art2_("Jonathan Mee"s);