初学者C ++ - 编译代码时遇到问题

时间:2017-09-20 21:01:13

标签: c++

编辑:以下工作代码

我会在下面粘贴我的代码,但如果有人可以向我解释为什么会发生什么事情而不是简单地写出答案,那就非常感谢,谢谢!

我有编译错误,告诉我有no instance of overloaded function。我可以通过注释掉cin.get(Kingdom.m_name, 32, '\n');行来绕过这个编译错误,但这显然没用,因为现在我的程序在输入第一个王国的名字后才会终止。

我假设行void read(sict::Kingdom& Kingdom)强制计算机根据用户输入在我的Kingdom数组中循环。

//header file
#ifndef KINGDOM_H
#define KINGDOM_H
#include <cstdlib>
// TODO: sict namespace
namespace sict
{
    // TODO: define the structure Kingdom in the sict namespace

        struct Kingdom {
            char m_name[32];
            int m_population;
        };
        // TODO: declare the function display(...),
        //         also in the sict namespace
        void display(const Kingdom& pKingdom);
        void display(const Kingdom kingdoms[], size_t count);
}


#endif

`

//implementation .cpp
#include <iostream>
#include <cstdlib>
#include "Kingdom.h"
using namespace std;
// TODO: the sict namespace
namespace sict
{
    // TODO:definition for display(...)

    void display(const Kingdom& pKingdom) {
        cout << pKingdom.m_name << ", " << "population " << pKingdom.m_population << endl;
    }

    void display(const Kingdom kingdoms[], size_t count) {
        cout << "------------------------------" << endl;
        cout << "Kingdoms of SICT" << endl;
        cout << "------------------------------" << endl;
        int pop = 0;
        for (size_t i = 0; i < count; i++) {
            cout << i + 1 << ". ";
            display(kingdoms[i]);
                pop += kingdoms[i].m_population;
        }
        cout << "------------------------------" << endl;
        cout << "Total population of SICT: " << pop << endl;
        cout << "------------------------------";
    }
}

我的主人,

    #include <iostream>
    #include <cstring> //for size_t definition
    #include <vector>
    #include "Kingdom.h"

    using namespace std;
    using namespace sict;

    void read(Kingdom&);

    int main() {
        int count = 0; // the number of kingdoms in the array

        // TODO: declare the pKingdom pointer here (don't forget to initialize it)
        Kingdom *pKingdom = nullptr;
        cout << "==========\n"
            << "Input data\n"
            << "==========\n"
            << "Enter the number of Kingdoms: ";
        cin >> count;
        cin.ignore();

        if (count < 1) return 1;

        // TODO: allocate dynamic memory here for the pKingdom pointer
        pKingdom = new Kingdom[count];
        for (int i = 0; i < count; ++i) {
            cout << "Kingdom #" << i + 1 << ": " << endl;
            // TODO: add code to accept user input for Kingdom i
            read(pKingdom[i]);
        }
        cout << "==========" << endl << endl;

        // testing that "display(...)" works
        cout << "------------------------------" << endl
            << "The 1st Kingdom entered is" << endl
            << "------------------------------" << endl;
        display(pKingdom[0]);
        cout << "------------------------------" << endl << endl;

        // expand the array of Kingdoms by 1 element
        count = count + 1;
        Kingdom *cpy_pKingdom = nullptr;
        // TODO: allocate dynamic memory for count + 1 Kingdoms
        cpy_pKingdom = new Kingdom[count];
        // TODO: copy elements from original array into this newly allocated array
        for (int i = 0; i < count; i++) {
            cpy_pKingdom[i] = pKingdom[i];
        }
        // TODO: deallocate the dynamic memory for the original array
        delete[] pKingdom;
        // TODO: copy the address of the newly allocated array into pKingdom pointer
        pKingdom = cpy_pKingdom;
        // add the new Kingdom
        cout << "==========\n"
             << "Input data\n"
             << "==========\n";
        cout << "Kingdom #" << count << ": " << endl;
             // TODO: accept input for the new element in the array
             read(pKingdom[count - 1]);
        cout << "==========\n" << endl;

        // testing that the overload of "display(...)" works
        display(pKingdom, count);
        cout << endl;

        // TODO: deallocate the dynamic memory here
        //delete[] pKingdom;
        //delete[] cpy_pKingdom;
        getchar();
        return 0;
    }

    // read accepts data for a Kingdom from standard input
    //
    void read(Kingdom& pkingdom) {
        cout << "Enter the name of the Kingdom: ";
        cin.get(pkingdom.m_name, 32, '\n');
        cin.ignore(2000, '\n');
        cout << "Enter the number of people living in " << pkingdom.m_name << ": ";
        cin >> pkingdom.m_population;
        cin.ignore(2000, '\n');

这可能是非常令人头痛的问题,而我的帽子也向那些已经完成了这一切的所有程序员开始了。

2 个答案:

答案 0 :(得分:1)

istream::get有六次重载。您传递的参数与其中任何一个都不匹配。这就是编译器无法编译该行的原因。

问题是您使用char作为name的{​​{1}}成员变量。这听起来不对。名称通常是字符串。它Kingdom不能用于表示名称。

您可以将char更改为name类型或std::string数组。如果使用char数组,则可以使用函数调用。

char

您对struct Kingdom { char m_name[32]; // Since you are passing 32 to cin.get int m_population; }; 使用std::string,您需要使用name

std::getline

和......

struct Kingdom {
    std::string m_name;
    int m_population;
};

我建议使用std::getline(std::cin, Kingdom.m_name); 。它们更容易使用。

答案 1 :(得分:0)

istream :: get具有以下重载(由http://en.cppreference.com/w/cpp/io/basic_istream/get提供):

(1)     int_type get();
(2)     basic_istream& get( char_type& ch );
(3)     basic_istream& get( char_type* s, std::streamsize count );
(4)     basic_istream& get( char_type* s, std::streamsize count, char_type delim );
(5)     basic_istream& get( basic_streambuf& strbuf );
(6)     basic_istream& get( basic_streambuf& strbuf, char_type delim );

你用三个参数调用get,并且在上面的重载之外,只有(4)匹配。

正如您所看到的,它期望一个char 指针作为它的第一个参数,在传递char时。 (m_name)

我认为您的意思是将m_name定义为char数组(例如char m_name [128]),因为这样会更合理。