C++ Code do not return vector as expected and throws errors on conversion

时间:2022-09-01 09:51:30

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;

}

1 个解决方案

#1


2  

Your GetAuthors() function has no return type.

您的GetAuthors()函数没有返回类型。

You should add std::vector<string> GetAuthors(){ ... }.

你应该添加std :: vector GetAuthors(){...}。

Apart from that, you should not use using namespace std;, use more specific names than SimpleClassand SecondClass and don't mix public declarations with private ones, keep it in one block.

除此之外,您不应该使用命名空间std;,使用比SimpleClass和SecondClass更具体的名称,并且不要将公共声明与私有声明混合,将其保存在一个块中。

#1


2  

Your GetAuthors() function has no return type.

您的GetAuthors()函数没有返回类型。

You should add std::vector<string> GetAuthors(){ ... }.

你应该添加std :: vector GetAuthors(){...}。

Apart from that, you should not use using namespace std;, use more specific names than SimpleClassand SecondClass and don't mix public declarations with private ones, keep it in one block.

除此之外,您不应该使用命名空间std;,使用比SimpleClass和SecondClass更具体的名称,并且不要将公共声明与私有声明混合,将其保存在一个块中。