分离类定义和实现:没有存储类或类型说明符[关闭]

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

我正在尝试编写一个类 Frame,定义转到 frame.h,实现转到 frame.cpp。但是,由于以下错误,代码无法编译:

g++ -c frame.cpp
frame.cpp:7:1: error: ‘frame’ does not name a type; did you mean ‘Frame’?
    7 | frame::Frame(int cols, int rows) {
      | ^~~~~
      | Frame
frame.cpp:19:6: error: ‘frame’ has not been declared
   19 | void frame::print() {
      |      ^~~~~
frame.cpp: In function ‘void print()’:
frame.cpp:21:35: error: ‘num_rows’ was not declared in this scope
   21 |     for (int iter_row=0; iter_row<num_rows; iter_row++){
      |                                   ^~~~~~~~
frame.cpp:22:39: error: ‘num_cols’ was not declared in this scope
   22 |         for (int iter_col=0; iter_col<num_cols; iter_col++) {
      |                                       ^~~~~~~~
frame.cpp:23:21: error: ‘content’ was not declared in this scope; did you mean ‘const’?
   23 |             cout << content[iter_col][iter_row];
      |                     ^~~~~~~
      |                     const
make: *** [Makefile:7: frame.o] Error 1

这是我到目前为止所得到的:

frame.h

#ifndef FRAME
#define FRAME

#include <bits/stdc++.h>

using namespace std;

class Frame {
    int num_cols;
    int num_rows;
    vector<vector<string>> content;
    public:
        Frame(int cols, int rows);
        void print();
};

#endif

frame.cpp

#include <bits/stdc++.h>
#include "clear.h"
#include "frame.h"

using namespace std;

frame::Frame(int cols, int rows) {
    num_cols = cols;
    num_rows = rows;
    for (int i=0; i<num_cols; i++) {
        vector<string> col = {};
        for (int i=0; i<num_rows; i++){
            col.push_back(" ");
        }
        content.push_back(col);
    }
};

void frame::print() {
    clear();
    for (int iter_row=0; iter_row<num_rows; iter_row++){
        for (int iter_col=0; iter_col<num_cols; iter_col++) {
            cout << content[iter_col][iter_row];
        }
    }
};

但是代码无法编译。 VS Code 将红色波浪线放在 frame::Frame 下警告我

this declaration has no storage class or type specifierC/C++(77)
。这是什么意思,我该如何更正我的代码?

c++ class header
© www.soinside.com 2019 - 2024. All rights reserved.