在类中定义映射 我是C++新手,不能正确定义类定义中的映射。< /P>

在类中定义映射 我是C++新手,不能正确定义类定义中的映射。< /P>,c++,data-structures,coding-style,map,C++,Data Structures,Coding Style,Map,以下是我编写的代码: #include<stdio.h> #include <iostream> #include <map> #include <utility> // make_pair #include <string.h> #include <sstream> using namespace std; class assgnt { private: //typedef std::map<int, i

以下是我编写的代码:

#include<stdio.h>
#include <iostream>
#include <map>
#include <utility> // make_pair
#include <string.h>
#include <sstream>

using namespace std;

class assgnt
{
private:
    //typedef std::map<int, int> MapType;
    //MapType my_map;
public:
    bool isGoodPoint(int x, int y);
    void addGoodNeighbours(int x, int y);

};

inline bool assgnt::isGoodPoint(int x, int y)
{
    string xs, ys;
    stringstream xout, yout;
    int xsum=0, ysum=0;

    xout<<x; yout<<y;

    xs = xout.str();
    ys = yout.str();

    for each (char c in xs)
    {
        xsum = xsum + int(c);
    }

    for each (char c in ys)
    {
        ysum = ysum + int(c);
    }

    if (xsum+ysum <= 19)
        return true;
    else 
        return false;
}

inline void assgnt::addGoodNeighbours(int x, int y)
{
    //if assgnt::isGoodPoint(x+1,y)
    if(isGoodPoint(x+1,y))
    {

    }
    /*if isGoodPoint(x+1,y) and [x+1,y] not in points: points.append([x+1,y])
    if isGoodPoint(x-1,y) and [x-1,y] not in points: points.append([x-1,y])
    if isGoodPoint(x,y+1) and [x,y+1] not in points: points.append([x,y+1])
    if isGoodPoint(x,y-1) and [x,y-1] not in points: points.append([x,y-1])*/
}


int main()
{

   typedef std::map<int, int> MapType;
    MapType my_map;

    // insert elements using insert function
    my_map.insert(std::pair<int, int>(0, 0));

    int i=0;

    while true
    {


    //my_map.insert(std::pair<int, int>(2, 2));
    //my_map.insert(std::pair<int, int>(3, 3));
    //my_map.insert(MapType::value_type(4, 4)); // all standard containers provide this typedef
    //my_map.insert(std::make_pair(5, 5));      // can also use the utility function make_pair

    MapType::iterator iter = my_map.begin();

    std::cout << "Size of my_map: " << my_map.size() << '\n';

    std::cout << "Enter a key to search for: ";
    int c;
    std::cin >> c;

    iter = my_map.find(c);
    if (iter != my_map.end()) 
    {
        int num = iter->second;
        if(num == 0)
            std::cout << "Value is found: " << iter->second << '\n';
        else 
            std::cout << "Value not found: ";
    }
    else
        std::cout << "Key is not in my_map" << '\n';

//    my_map.clear();
}
#包括
#包括
#包括
#包括//make\u配对
#包括
#包括
使用名称空间std;
班级助理
{
私人:
//typedef std::map MapType;
//地图输入我的地图;
公众:
bool-isGoodPoint(intx,inty);
void addgoodneights(整数x,整数y);
};
内联bool assgnt::isGoodPoint(intx,inty)
{
字符串xs,ys;
stringstream xout,yout;
int xsum=0,ysum=0;

xout我想你想在类外使用它?在一个公共部分定义它,你会很高兴的。从封装的角度来看,typedef是绝对安全的,它们只是同义词。然后你可以在
main
中使用它作为
assgnt::MapType

看起来你的编译问题不在于你的地图,而在于你的while syntax:

-while true
+while(true)
{

+}

要使代码正常工作,您需要解决以下几个问题:

  • 您的类需要一个公共构造函数
  • 公开你的地图
  • 在类外使用映射时,请使用类型assgnt::MapType
  • 这将给你工作代码,这样你就可以尝试你正在做的任何事情。我想这就是你的问题所在?你不能让它在课堂上工作


    代码中还有一些其他错误,但您应该能够找出这些错误;)

    您的
    循环包含一个错误:它应该是

    while(true)
    {
        //...
    }
    
    Infinete循环是一个坏主意(您的程序不会终止)-您没有编写任何像这样的条件语句

    while(true)
    {
        //...
        if(some_condition)
            break;
    }
    
    您需要将您的
    my_map
    字段公开,以便您可以从
    main
    (以及其他过程)访问它

    最后实例化您的类:

    int main()
    {
        assgnt obj;
        //...
        // now write obj.my_map instead of just my_map
    }
    

    如果main()中有“typedef std::map MapType;MapType my_map;”,那么代码可以正常工作。但是我希望在我定义的内联函数中使用MapType,因此我希望在类的私有部分声明“typedef std::map MapType;”。即使这样也可以正常工作,但在使用main()中的“MapType my_map”给了我一个错误。简言之,我希望在类中声明我的std::map并在内联函数中使用它。我可以这样做吗?可能吗?谢谢你的问题:)是的,这是我的问题。谢谢我做到了…:)