我正在开发一个用C ++编写的操作系统,可以在我自己的虚拟机上运行,也可以用C ++编写。我修改了原始版本以允许时间片设置同时运行多个程序。
我遇到的问题是编译os代码时。我收到以下错误:
In file included from os.h:12,
from os.cpp:10:
VirtualMachine.h:126: error: expected identifier before â;â token
VirtualMachine.h:126: error: friend declaration does not name a class or function
In file included from os.cpp:10:
os.h:19: error: expected unqualified-id before â)â token
os.h:33: error: expected identifier before â}â token
os.h:33: error: expected unqualified-id before â}â token
os.h:17: error: an anonymous struct cannot have function members
os.h:23: error: member âAssembler <anonymous class>::asâ with constructor not allowed in anonymous aggregate
os.h:23: error: member âAssembler <anonymous class>::asâ with destructor not allowed in anonymous aggregate
os.h:23: error: member âAssembler <anonymous class>::asâ with copy assignment operator not allowed in anonymous aggregate
os.h:24: error: member âVirtualMachine <anonymous class>::vmâ with constructor not allowed in anonymous aggregate
os.h:24: error: member âVirtualMachine <anonymous class>::vmâ with destructor not allowed in anonymous aggregate
os.h:24: error: member âVirtualMachine <anonymous class>::vmâ with copy assignment operator not allowed in anonymous aggregate
我相信这是因为我在os类中使用虚拟机类。 我已经使虚拟机成为了操作系统的朋友类,但似乎我仍然无法访问虚拟机下的功能。
OS.h
#ifndef OS
#define OS
#include "Assembler.h"
#include "VirtualMachine.h"
#include <queue>
#include <list>
#include <iomanip>
class OS{
public:
OS();
void run();
private:
void assembler_all();
Assembler as;
VirtualMachine vm;
PCB * running;
list<PCB *> pcb;
list<PCB *> ended_jobs;
queue<PCB *> waitq;
queue<PCB *> readyq;
queue<PCB *> runq;
};
#endif
Assembler.h
#ifndef ASSEMBLER
#define ASSEMBLER
#include <vector>
#include <string>
#include <cstdlib> // for exit()
#include <iostream> // for cout, ...
#include <fstream> // for fstream::open()
#include <sstream> // for istringstream
#include <bitset>
#include <algorithm>
#include <map>
using namespace std;
class Assembler{
private:
static const int REG_MAX = 3;
static const int MEM_SIZE = 256;
string opcode;
fstream assemblyProg; //reading the .s file
string line, a_string, outname, inname;
int rd, rs, constant, avalue, a_value, addr, mem_used, mem_current;
vector<int> memory;
public:
void build(string filename);
void reg_check(int val_reg);
void val_check(int val);
void addr_check(int val);
};
#endif
VirtualMachine.h
/*
Filename: VirtualMachine.h
Authors:
Description: This file contains the 26 defined functions for our operating system
as well as helper functions for out virtual machine. Souce code for the functions can be
found in the VirtualMachine.cpp file.
*/
#ifndef VIRTUALMACHINE
#define VIRTUALMACHINE
#include <list>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <stdio.h>
using namespace std;
class format1 {
public:
unsigned UNUSED:6;
unsigned RS:2;
unsigned I:1;
unsigned RD:2;
unsigned OP:5;
};
class format2 {
public:
unsigned ADDR:8;
unsigned I:1;
unsigned RD:2;
unsigned OP:5;
};
class format3 {
public:
int CONST:8;
unsigned I:1;
unsigned RD:2;
unsigned OP:5;
};
union instruction {
int i;
format1 f1;
format2 f2;
format3 f3;
};
struct PCB{
vector<int> r;
int pc,sr,sp,base,limit, IO_clock;
int CPU_time, largest_stack_size, ta_time, io_time, waiting_time;
string pName;
ifstream pcb_in;//.in
ofstream pcb_out;//.out
fstream pcb_st;//.st
};
class VirtualMachine{
public:
VirtualMachine();
void loads();
void store();
void adds();
void addc();
void subs();
void subc();
void ands();
void xors() ;
void compls();
void shl();
void shla();
void shr();
void shra();
void compr();
void getstat();
void putstat();
void jump();
void jumpl();
void jumpe();
void jumpg();
void call();
void returns();
void read();
void write();
void halt();
void noop();
void setoverflow();
bool iscarry();
void setcarry();
// void run(string);
int get_status() {return ((sr & 0xE0) >> 5);}
void run(PCB *);
void savePCB(PCB *);
void loadPCB(PCB *);
void loadMem(list<PCB *> &);
private:
typedef void (VirtualMachine::*FP)();
vector<FP> functions;
static const int REG_SIZE = 4;
static const int MEM_SIZE = 256;
static const int FMAP_SIZE = 26;
vector<int> mem;
vector<int> r;
ifstream infile;
ifstream infile2;
ofstream outfile;
string wfile;
string rfile;
int pc, limit, sr, clock, sp, base,mem_used, ir, time_slice;
instruction lineObject;
PCB * current;
friend class OS;
};
#endif
我需要修改什么才能允许操作系统使用汇编程序和虚拟机类?
答案 0 :(得分:1)
一个问题是你有一个宏#define OS
。宏是文本替换,因此行friend class OS;
转换为friend class ;
,这是语法错误。
要解决此问题,请使用不会与您的类名冲突的包含警卫的名称。我使用H_ALLUPPER作为标头包含保护,并且除了预处理器宏之外,不要使用全大写的其他任何东西。