我正在尝试制作俄罗斯方块游戏而且我遇到了编译器错误
Shape is not an enclosing class
当我尝试创建一个对象时
public class Test {
public static void main(String[] args) {
Shape s = new Shapes.ZShape();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在为每个形状使用内部类.这是我的代码的一部分
public class Shapes {
class AShape {
}
class ZShape {
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
我在以下代码中有一个奇怪的错误:
float Student::getAverageMark() const throw (NoMarkException)
{
int sum = 0;
int count = 0;
for(map<string, float>::iterator iter = marks.begin(); iter != marks.end(); ++iter) {
sum += iter->second;
count++;
}
return sum/count;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它没有什么不同寻常的,它是一个计算地图中平均标记的简单代码.我在一个在线编译器中对它进行了测试并且它有效,但是当我尝试在我的机器上编译它时(我正在使用带有GNU GCC编译器的CodeBlocks)我收到此错误:
错误:从'std :: map,float> :: const_iterator {aka std :: _ Rb_tree_const_iterator,float >>}'转换为非标量类型'std :: map,float> :: iterator {aka std :: _ Rb_tree_iterator, float >>>''request |
我在弄清楚这个错误时遇到了一些麻烦.所以我有一个Person类和一个Student子类.
Person类具有以下构造函数:
Person(const string &name)
{ this->name = name;
}
Run Code Online (Sandbox Code Playgroud)
Student类具有以下构造函数:
Student::Student(const string &name, int regNo)
{ this->name = name;
this->regNo = regNo;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译Student类时,我得到了构造函数的错误:
在构造函数'Student :: Student(const string&,int)'中:
错误:没有匹配函数来调用'Person :: Person()'
我是C++的新手,所以我不知道为什么会出现这个错误,但它显然与Person的继承有关.
我正在处理的任务有点问题.基本上,我有一个文件,学生证和他们的名字采用以下格式:
17987贝丝
17950克拉克
17936亚伦
我把文件的内容放在一个数组中,我需要按名称排序,而不是ID.如果我使用Arrays.sort(myArray)它将按ID自动排序.我很难理解比较器,所以如果你能逐步解释它,对我来说会更容易.谢谢!
所以我有一个抽象超类 ReadWords,和 3 个子类,FirstFilter、SecondFilter 和 ThirdFilter。
Readwords.h:
#ifndef _READWORDS_H
#define _READWORDS_H
using namespace std;
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
class ReadWords
{ public:
ReadWords(char *filename);
void close();
string getNextWord();
bool isNextWord();
virtual bool filter(string word)=0;
string getNextFilteredWord();
private:
ifstream wordfile;
bool eoffound;
string nextword;
string fix(string word);
};
#endif
Run Code Online (Sandbox Code Playgroud)
第一个过滤器.h:
#ifndef _FIRSTFILTER_H
#define _FIRSTFILTER_H
using namespace std;
#include <string>
#include <fstream>
#include <iostream>
#include "ReadWords.h"
class FirstFilter: public ReadWords
{ public:
FirstFilter(char *filename);
virtual bool filter(string word) …Run Code Online (Sandbox Code Playgroud)