Reputation: 9173
I have written a block of Code.
What it does? It prints something, then ask the user to insert the name of his favorite author, and then add the inserted author into a vector.
In the two last lines of the code, respectively, the vector of the authors is assigned to a vector variable, but upon this line, it throws the following code:
error C2664: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(std::initializer_list<_Elem>,const std::allocator<char> &)' : cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &'
It thus does not reach to the last line.
However, my problem is that, I want to return a vector of authors to which an author is added by the user. And this causes the problem, since prior to this, the code was working.
Here is the code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;
class SimpleClass
{
public: string AuthorName;
public: string AuthorLastWork;
vector<string> AuthorNames;
public: int AuthorBirthYear;
private: string AuthorBestSelling;
private: int AuthorNumOfSoldWorks;
public:
void SetAuthorName(string Name)
{
this->AuthorName = Name;
string namesOfAuthors[2] {"Jack London", "Flaubert"};
if (Name == namesOfAuthors[0])
{
this->setAuthorSoldWorks(2000000);
}
else if (Name == namesOfAuthors[1])
{
this->setAuthorSoldWorks(5000);
}
}
int GetAuthorNumOfSoldWorks()
{
return this->AuthorNumOfSoldWorks;
}
private :
void setAuthorSoldWorks(int number)
{
this->AuthorNumOfSoldWorks = number;
}
}; // END OF CLASS
class SecondClass : public SimpleClass
{
public:
void AddAuthor(string name)
{
this->AuthorNames.push_back(name);
}
GetAuthors(string name)
{
return this->AuthorNames;
}
};
int main()
{
string AuthorNameByUser;
SimpleClass SC;
SecondClass SecondClass;
SC.SetAuthorName("Jack London");
cout << "This is our selected Author: " << SC.AuthorName << endl;
cout << "Number of sold works: " << SC.GetAuthorNumOfSoldWorks() << " works." << endl;
cout << endl;
cout << "Please type the name of your favorite author: ";
getline(cin, AuthorNameByUser);
SecondClass.AddAuthor(AuthorNameByUser);
vector<string> AuthorsCollection = SecondClass.GetAuthors();
cout << "Thank for your particpiation. You have entered \"" << AuthorsCollection[0] << "\"" << endl;
}
Upvotes: 0
Views: 269
Reputation: 147
Your GetAuthors()
function has no return type.
You should add std::vector<string> GetAuthors(){ ... }
.
Apart from that, you should not use using namespace std;
, use more specific names than SimpleClass
and SecondClass
and don't mix public declarations with private ones, keep it in one block.
Upvotes: 2