【发布时间】:2026-01-18 01:50:01
【问题描述】:
我想用一些静态方法实现接口 Formatter,然后创建另一个实现这些方法的类。我会这样做:
Formatter.hpp
#include <iostream>
#include <string>
#include "Matrix.hpp"
#ifndef FORMATTER_HPP
#define FORMATTER_HPP
class Formatter {
public:
/**
* Retorna uma string que pode ser usada no symbolab para testes
*/
static std::string matrixString(Matrix* matrix);
/**
* Retorna string para symbolab para transposição da matriz
*/
static std::string transposedMatrixString(Matrix* matrix);
/**
* Retorna string para symbolab da soma de duas matrizes
*/
static std::string matrixSumString(Matrix* m1, Matrix* m2);
/**
* Retorna string para symbolab do produto de duas matrizes
*/
static std::string matrixProductString(Matrix* m1, Matrix* m2);
/**
* Retorna string para symbolab da determinante de uma matriz
*/
static std::string matrixDeterminantString(Matrix* matrix);
};
#endif
SymbolabFormatter.hpp
#include <iostream>
#include "Formatter.hpp"
#ifndef SYMBOLABFORMATTER_HPP
#define SYMBOLABFORMATTER_HPP
class SymbolabFormatter: public Formatter{
public:
SymbolabFormatter();
};
#endif
SymbolabFormatter.cpp
#include "SymbolabFormatter.hpp"
#include <sstream>
std::string SymbolabFormatter::matrixString(Matrix* matrix) {
...
}
std::string SymbolabFormatter::transposedMatrixString(Matrix *matrix) {
...
}
std::string SymbolabFormatter::matrixSumString(Matrix *m1, Matrix *m2) {
...
}
std::string SymbolabFormatter::matrixProductString(Matrix *m1, Matrix *m2) {
...
}
std::string SymbolabFormatter::matrixDeterminantString(Matrix* matrix) {
...
}
但不起作用。当我编译它时,会显示以下错误: Image from compiler
PS.: 抱歉有些语法错误,我的英语很糟糕。
【问题讨论】:
-
为什么你的
#include守卫没有放在你的头文件的顶部? -
我是 C++ 新手...我不明白您的问题...我猜 #include 是在正确的位置...如果不是,那么放在哪里?
-
我说的是 #ifndef FORMATTER_HPP / #define FORMATTER_HPP 和类似的行(你说你是 C++ 的新手,但显然不知道那些被称为包含警卫)。打开任何标准标题,例如
或 。你在哪里看到这些包括警卫? en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Include_Guard_Macro -
@PaulMcKenzie 感谢您的解释。
标签: c++ inheritance interface