我正在编写代码以分离接口及其实现,但是它有一个错误,我遵循接口和类的正确语法,我在这个主题上看到了许多示例,知道我是前C#程序员。 / p>
代码:
#include "stdafx.h"
#include<iostream>
#include <String>
using namespace std;
///////////////////EMPLOYEE CLASS AND INTERFACE //////////////
class Interface_Employee
{
public:
Interface_Employee() {}
virtual ~Interface_Employee() {}
virtual void print() = 0; // "= 0" part makes this method pure virtual, and
// also makes this class abstract.
};
class Employee : public Interface_Employee
{
int ID, Age;
Bank ac1, ac2; //composition
public:
Employee(int a, int b, Bank account1, Bank account2)
: ac1(account1), ac2(account2)
{
ID = a; Age = b;
}
~Employee() {}
// Provide implementation for the first method
void print()
{
cout << "Age is:/n" << Age << "ID is:" << ID;
ac1.print();
ac2.print();
}
};
////////////BANK CLASS AND INTERFACE//////////////////
class Interface_Bank
{
public:
Interface_Bank() {}
virtual ~Interface_Bank() {}
virtual void print() = 0; // "= 0" part makes this method pure virtual, and
// also makes this class abstract.
};
class Bank : public Interface_Bank
{
private:
int balance, ID;
public:
Bank(int a, int b)
{
balance = a;
ID = b;
}
~Bank() {}
// Provide implementation for the first method
void print()
{
cout << "Balance is:/n" << balance << "ID is:" << ID;
}
};
class Output {
int main(void)
{
Bank a(1000, 1);
Bank b(2000, 2);
Employee c(2, 25, a, b);
return 0;
}
};
错误:
严重性代码描述项目文件行抑制状态
错误C3646 'ac1':未知的覆盖说明符 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 22
错误C2059 语法错误:',' console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 22
错误C2238 ';' 之前的意外令牌(console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 22
错误 C2061语法错误:标识符“银行” console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 24
错误C2065 'account1':未声明的标识符 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 25
错误C2065 'account2':未声明的标识符 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 25
错误C2614 'Employee':非法成员初始化:'ac1'不是基础或成员 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app .cpp 25
错误C2614 'Employee':非法成员初始化:'ac2'不是基础或成员 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app .cpp 25
错误 C2065'ac1':未声明的标识符 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 37
错误C2228'.print'左侧的 必须具有class / struct / union 。console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 37 < br />
错误C2065 'ac2':未声明的标识符 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 38
错误C2228'.print'左侧的 必须具有class / struct / union 。console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 38 < br />
错误C2661 'Employee :: Employee':没有重载函数需要4个参数 console_App c:\ users \ dell_alrfou \ source \ repos \ console_app \ console_app \ console_app.cpp 81
答案 0 :(得分:1)
在尝试使用Bank
类之前,您需要对其进行声明或定义。
您需要将Bank
(及其Interface_Bank
基类)的定义移到class Employee
之前。
答案 1 :(得分:0)
Bank
之前,您应该对其进行声明或定义int main()
不应放在class
以下code
可以工作:
#include<iostream>
#include <String>
using namespace std;
class Interface_Bank
{
public:
Interface_Bank() {}
virtual ~Interface_Bank() {}
virtual void print() = 0;
};
class Bank : public Interface_Bank
{
private:
int balance, ID;
public:
Bank(int a, int b) : balance(a), ID(b)
{
}
~Bank() {}
void print()
{
cout << "Balance is:/n" << balance << "ID is:" << ID;
}
};
class Interface_Employee
{
public:
Interface_Employee() {}
virtual ~Interface_Employee() {}
virtual void print() = 0;
};
class Employee : public Interface_Employee
{
private:
int ID, Age;
Bank ac1, ac2;
public:
Employee(int a, int b, Bank account1, Bank account2)
: ID(a), Age(b), ac1(account1), ac2(account2)
{
}
~Employee() {}
void print()
{
cout << "Age is:/n" << Age << "ID is:" << ID;
ac1.print();
ac2.print();
}
};
int main(void)
{
Bank a(1000, 1);
Bank b(2000, 2);
Employee c(2, 25, a, b);
c.print();
return 0;
}