当我尝试创建此类的对象时,为什么会出现“找不到标识符”错误

问题描述 投票:0回答:1

我已经声明了这个类,创建并包含了它的头文件和构造函数,但由于某种原因它仍然不允许我构造一个对象。

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include "gameBoard.h"

int main()
{
    std::cout << "Hello World!\n";
    std::ifstream readFile;
    std::string filename;
    std::vector<std::vector<std::string>> gameBoard;

    std::cout << "Enter the name of the file: ";
    std::cin >> filename;
    readFile.open(filename);

    gameBoard current(readFile); <-- expected a semicolon error
}

我收到以下错误: “预计有一个‘;’” “语法错误:缺少‘;’在标识符“当前”之前 “‘当前’:找不到标识符”

这是构造函数:

#include "gameBoard.h"
#include<iostream>

gameBoard::gameBoard(std::fstream file)
{
    std::string line;
    while (getline(file, line))
    {
        std::vector<std::string> row;
        std::stringstream s(line);
        std::string val;
        while (getline(s, val, ','))
        {
            row.push_back(val);
        }
        layout.push_back(row);
    }
}

void gameBoard::output()
{
    for (auto& row : layout)
    {
        for (auto& val : row)
        {
            std::cout << val << " ";
        }
        std::cout << std::endl;
    }
}

这是头文件:

#pragma once
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

class gameBoard
{
private:
    std::vector<std::vector<std::string>> layout;

public:
    gameBoard(std::fstream file);
    void output();
};

也许你们可以帮助我?

我试图让这个对象读取 csv 文件并将其数据存储到二维向量中。 我之前在 main 的类之外编写了所有相同的代码来测试它,并且它在那里完美地工作。我想知道为什么现在不起作用。

c++ class object
1个回答
0
投票

您已在

gameBoard
main()
声明了 3 次:

  • 作为一种类类型从
    gameBoard.h
  • 拉入
  • one 作为名为
    gameBoard
    类型为
    std::vector
  • 的对象
  • one 作为名为
    current
    类型为
    gameBoard
    的对象。

std::vector
声明没有理由存在于
main()
中,将其删除。

© www.soinside.com 2019 - 2024. All rights reserved.