自从我用 C++ 编写代码以来已经有一段时间了,所以我一直在查看一些旧代码只是为了开始我的程序。
我在名为“branchsim.hpp”的文件中包含以下内容:
#ifndef _BRANCHSIM_HPP
#define _BRANCHSIM_HPP
// some #include statements here
using std::cin;
using std::cout;
// ... some more using statements here
class BranchSim {
public:
int readMode(&string mode);
};
#endif
我在名为 branchsim.cpp 的文件中有这个:
#include "./branchsim.hpp"
int main(void) {
string mode;
return EXIT_SUCCESS;
}
/*
* Returns an int representing the branch prediction heuristic to be
used.
*/
int BranchSim::readMode(&string mode) {
switch (tolower(mode)) {
case "at": // always taken
return 1;
case "nt": // never taken
return 2;
case "btfn": // bkwd taken, fwd not taken
return 3;
case "bimodal": // bimodal
return 4;
case "twolevel": // dynamic two level
return 5;
default:
cout << "Invalid prediction mode entered.\n";
}
return 0;
}
我收到以下错误:
./branchsim.cpp:35:24: error: ‘int BranchSim::readMode’ is not a
static data member of ‘class BranchSim’
int BranchSim::readMode(&string mode) {
^
./branchsim.cpp:35:33: error: expected primary-expression before
‘mode’
int BranchSim::readMode(&string mode) {
^
./branchsim.cpp:35:39: error: expected ‘,’ or ‘;’ before ‘{’ token
int BranchSim::readMode(&string mode) {
^
Makefile:16: recipe for target 'branchsim.o' failed
make: *** [branchsim.o] Error 1
我只是想让它编译,这样我就可以继续编写程序的其余部分,但我遇到了困难。
我相信我的语法是正确的..我只是有点不确定如何修复这些错误。正如我所说,我的 C++ 有点生疏,所以这很可能是我遗漏/忘记的很明显的东西,但我们将不胜感激任何帮助!
编辑:我只是错放了 &(属于字符串之后,而不是之前),我很快意识到 c++ 不允许打开字符串,所以我进行了相应的调整。
对于这个微不足道的问题深表歉意,但感谢您的快速帮助!
最佳答案
您的意思可能是 string& mode
在“对字符串的引用”中,而不是 &string mode
在“字符串的地址”中。典型的 C++ 代码的签名如下所示:
int BranchSim::readMode(const string& mode);
实现遵循相同的规范。
关于C++ 小错误 : "... is not a static data member of ‘class ...' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519015/