Class and string question

Hi everyone,
I recently got an assignment that requires me to use a class and strings to make a program about a movie. The exact question is...

"Define a class named Movie. Include private fields for the title, year, and name of the director. Include three public functions with the prototypes void Movie::setTitle(string); , void Movie::setYear(int); , and void setDirector(string); . Include another function that displays all the information about a Movie. Write a main() function that declares a movie object named myFavoriteMovie . Set and display the object’s fields."

... So far, all i have is below and i am having trouble with the strcpy function. I get the error message "error C2664: 'char*strcpy(char*,const char*)': cannot convert argument 1 from 'std::string' to 'char*' "...

Here is my code, let me know what you think.
Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include<conio.h>


using namespace std;

class Movie
{
private:

	string title;
	int year;
	string director;

	

public:
	void setTitle(string);
	void setYear(int);
	void setDirector(string);

	void displayMovie();

};

void Movie::setTitle(string newtitle)
{
	strcpy(title, newtitle);

}
I'm not completely sure, but try putting 'string title' in public.
Sry if it doesnt help

Also , could you give the rest of your code?
Last edited on
I'm not completely sure, but try putting 'string title' in public.


No, that's not a solution: data should be private. Member functions have direct access to private class members.

@brudz13

Why are you using strcpy?

title = newtitle;

Good Luck !!
@TheIdeasMan

Using strcpy because that's all I know how to use in this case. Im relatively new to this. Is there a better option to use other than strcpy?

Thanks.
strcpy is when you're using old-fashioned C-style strings, i.e. char arrays.

You're using the C++ std::string class for strings, so you don't need to use strcpy. That's the reason for your error - you're trying to pass a std::string, when strcpy expects a char array.
Last edited on
Here is how to copy a C++ string.

1
2
3
4
5
string original("beans");
string copy;

// now do the copy, so that the string "copy" is the same as the string "original"
copy = original;
Topic archived. No new replies allowed.