0

所以我在几个文件中有一些代码:cells.cpp:

#include "cells.h"
#include <iostream>
using namespace std;

char convertIntChar (int symbolNumber)
{
    char charR;
    switch (symbolNumber)
    {
    case 0:
        charR='0';
        break;
// lust of case code here
    case 63:
        charR='\\';
        break;
    }
    return charR;
}

class cell
{
public:
    int iPosition;
    char chPosition;
    cell ()
    {
        static int i = -1;
        i++;
        chPosition=convertIntChar (i);
        iPosition=i;
        cout << " " << iPosition;  //two lines of code to test 
        cout << " " << chPosition; //constructor
    }
protected:
};

主文件

#include <iostream>
#include "cells.h"
#include "pointer.h"
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    createPointer();
    cell cells[64];
    return 0;
}

还有一个cells.h

#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
#pragma once
class cell
char convertIntChar(int symbolNumber);
#endif // CELLS_H_INCLUDED

我有一个错误,听起来像 //filepath\|5|error: two or more data types in declaration of 'convertIntChar'| ||=== 构建完成:1 个错误,0 个警告(0 分 7 秒)===| 会是什么。无论如何,对不起菜鸟问题。

4

2 回答 2

2

首先,这个前向声明需要一个分号:

class cell;
//        ^

其次,您不能在此处使用前向声明。main.cpp需要查看cell类定义。所以你应该把定义放在cells.h. 例如:

cells.h

#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
class cell
{
public:
    int iPosition;
    char chPosition;
    cell ();
};

char convertIntChar(int symbolNumber);

#endif

cells.cpp

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

char convertIntChar (int symbolNumber)
{
    char charR;
    // as before
    return charR;
}


cell::cell ()
{
    static int i = -1;
    i++;
    chPosition=convertIntChar (i);
    iPosition=i;
    std::cout << " " << iPosition;  //two lines of code to test 
    std::cout << " " << chPosition; //constructor
}
于 2013-06-23T13:35:34.827 回答
0

class cell在 cpp 文件中应该进入 .h 文件。

然后在 cells.h 中你缺少;after class cell

在 cell.h 中的前向声明的 Insterad,将类放在那里。

于 2013-06-23T13:36:04.340 回答