为什么我有“预期的类型说明符错误”?

问题描述 投票:-4回答:1

我在主cpp文件中的单词Node上有一个“预期类型说明符错误”a = new Node('A)。有人可以告诉我为什么会这样,也可能如何修复它?我到处都看,没什么似乎解决了这个问题。该程序旨在制作一个用户必须浏览的节点AL的迷宫。

头文件:

#pragma once
#ifndef NODE_H
#define NODE_H
#include <string>

using namespace std;

namespace mazeGraph
{ 
    class Node
    {
    public: 
        Node();
        Node(char newNode);
        char getName() const;
        Node *getAdjacentRoom(char direction) const;
        void edge(char direction, Node *other);
        string getMovementOptions();
    private: 
        char roomName;
        Node *north, *west, *south, *east;
    };
    typedef Node *nodeptr;
}

#endif

Room.cpp:

#include "stdafx.h"
#include "Room.h"
#include <string>
using namespace std;

namespace mazeGraph
{
    Node::Node() : roomName(' '), north(NULL), south(NULL), east(NULL), west(NULL)
    {
    }
    Node::Node(char newNode) : roomName(newNode), north(NULL), south(NULL), east(NULL), west(NULL)
    {
    }
    char Node::getName() const
    {
        return roomName;
    }
    Node* Node::getAdjacentRoom(char direction) const
    {
        switch (direction)
        {
        case 'N':
            return north;
        case 'S':
            return south;
        case 'E':
            return east;
        case 'W':
            return west;
        }
        return NULL;
    }
    void Node::edge(char direction, Node * other)
    {
        switch (direction)
        {
        case 'N':
            north = other;
            break;
        case 'S':
            south = other;
            break;
        case 'E':
            east = other;
            break;
        case 'W':
            west = other;
            break;
        }
    }
    string Node::getMovementOptions()
    {
        string movement = "";
        if (north != NULL)
            movement = "N: North ";
        if (south != NULL)
            movement = "S: South ";
        if (east != NULL)
            movement = "E: East ";
        if (west != NULL)
            movement = "W: West ";
        return movement;
    }
}

主cpp文件:

#include "stdafx.h"
#include <string>
#include "Room.h"
#include "Room.cpp"
#include <iostream>

using namespace std;

int main()
{
    mazeGraph::nodeptr a, b, c, d, e, f, g, h, i, j, k, l;
    a = new Node('A');
    b = new Node('B');
    c = new Node('C');
    d = new Node('D');
    e = new Node('E');
    f = new Node('F');
    g = new Node('G');
    h = new Node('H');
    i = new Node('I');
    j = new Node('J');
    k = new Node('K');
    l = new Node('L');
    return 0;
}
c++ visual-studio nodes
1个回答
2
投票

你忘了做mazeGraph::Node。类Node位于命名空间mazeGraph中,因此编译器在命名空间之外不知道它。

a = new mazeGraph::Node('A');
© www.soinside.com 2019 - 2024. All rights reserved.