首页 > 解决方案 > 如何以自定义类型的二维向量作为参数正确定义类成员函数?(C++)

问题描述

目前,当我尝试定义一个使用自定义类型的二维向量作为其参数之一的类成员函数时,我的代码中出现“声明不兼容”错误。这是我的类文件的精简版本,仅包含问题函数:

#include <vector>
#include "sudoku_structs.cpp" //This is the file where I define my custom type called point

namespace std{
     class Square{
          private:
               bool checkCell(char num, int row, int col, vector<vector<point>> grid);
     };
}

这是我在 sudoku_structs 文件中定义我的类型的地方:

typedef struct Point{
    char val;
    bool canModify, modified;
    Point();

} point;

Point::Point(){
    val = '0';
    canModify = false;
    modified = false;
}

这是我的 square_functions 文件的精简版本,其中包含问题行:

#include <vector>

#include "sudoku_structs.cpp"
#include "square_class.cpp" //Class file for the Square class that I showed previously

namespace std{
//Problem Line is the line Below
     bool Square::checkCell(char num, int row, int col, vector<vector<point>> grid){
          /*Cell-Checking Code*/
     }
}

具体来说,我的编译器/vscode 给出的错误是:

声明与“bool std::Square::checkCell(char num, int row, int col, std::vector<std::vector<point, std::allocator>, std::allocator<std::vector <point, std::allocator>>> grid)"(在第 29 行声明)C/C++(147)

我实际上正在处理重构工作代码,而这种自定义类型是该过程的一部分。该函数用于使用 2d 字符向量,而不是我现在使用的这种自定义类型,并且效果非常好。因此,我认为问题必须与命名空间或我如何定义点类型和/或 Square 成员函数有关,但我似乎无法找到/想出一个有效的解决方案,所以任何帮助都会不胜感激。

另外,如果我没有提供足够的代码来获取正确的上下文/找出问题,请告诉我,我是该网站的新手,我真的不知道我应该/不应该在这里倾倒多少代码,如果需要,我很乐意用更多代码发表评论,或者链接到 github 页面。

标签: c++

解决方案


推荐阅读