鱼C论坛

 找回密码
 立即注册
查看: 1133|回复: 6

[已解决]【在线等急】求助!

[复制链接]
发表于 2023-4-1 10:36:20 | 显示全部楼层 |阅读模式
10鱼币

Arithmetic.h:
  1. #pragma once

  2. #ifndef _ARITHMETIC_
  3. #define _ARITHMETIC_

  4. #define __ARITHMETIC_API_BEGIN namespace api {
  5. #define __ARITHMETIC_API_END   }
  6. #define _API                   api::

  7. #include <iostream>
  8. #include <string>
  9. #include <cstdlib>
  10. #include <stack>

  11. __ARITHMETIC_API_BEGIN

  12. typedef       void*       idc_pointer;
  13. typedef       AccountType account_type;
  14. typedef const Account     const_acc,    *const_acc_pointer;
  15. typedef const SubAccount  const_subacc, *const_subacc_pointer;

  16. enum EventType
  17. {
  18.         SystemEvent, // 系统消息
  19.         OtherEvent,  // 其他消息
  20. };

  21. enum AccountType
  22. {
  23.         SYSTEM,
  24.         ADMINISTRATOR,
  25.         STANDARD_USER,
  26.         BAN_USER,
  27.         ALL_BAN_USER,
  28. };

  29. struct Event
  30. {
  31.         EventType    event_type;
  32.         std::wstring event;
  33. };

  34. class IDC
  35. {
  36. private:
  37.         const char id[18];
  38.         const char    key;
  39. };

  40. class Account
  41. {
  42. public:
  43.         Account(void)  = default;
  44.         ~Account(void) = default;

  45.         std::wstring name; // 昵称

  46.         static void*     operator new(size_t size);      //     重  载     //
  47.         static void   operator delete(void* pointer);   //     运算符     //
  48.         static void*   operator new[](size_t size);    //     函  数     //
  49.         static void operator delete[](void* pointer); // new and delete //

  50.         template <idc_pointer* idc>
  51.         Account operator +(const_acc acc);

  52.         template <idc_pointer* idc>
  53.         Account operator -(const_acc acc);

  54. protected:
  55.         long long int uid; // 用户 ID

  56. private:
  57.         unsigned long long int score = 0;
  58.         unsigned long long int exp   = 0;

  59.         std::stack <Event> event_stack;

  60.         account_type acc_type = STANDARD_USER;

  61.         friend std::wostream& operator <<(std::wostream& wos, const_acc acc);
  62.         friend std::wistream& operator >>(std::wistream& wis, const_acc acc);
  63. };

  64. class SubAccount : public Account
  65. {
  66. public:
  67.         SubAccount(void)  = default;
  68.         ~SubAccount(void) = default;

  69. private:
  70.         unsigned long long int sub_uid; // 子用户 ID
  71. };

  72. __ARITHMETIC_API_END

  73. #endif
复制代码


Arithmetic.cpp:
  1. #include "Arithmetic.h"

  2. __ARITHMETIC_API_BEGIN

  3. void* Account::operator new(size_t size)
  4. {
  5.     return malloc(size);
  6. }

  7. void Account::operator delete(void* pointer)
  8. {
  9.     free(pointer);
  10. }

  11. void* Account::operator new[](size_t size)
  12. {
  13.     return malloc(size);
  14. }

  15. void Account::operator delete[](void* pointer)
  16. {
  17.     free(pointer);
  18. }

  19. template <bool printEvent = false>
  20. std::wostream& operator<<(std::wostream& wos, const_acc acc)
  21. {
  22.     return wos;
  23. }

  24. template <bool inputLevel = false>
  25. std::wistream& operator>>(std::wistream& wis, const_acc acc)
  26. {
  27.     return wis;
  28. }

  29. template <idc_pointer* idc>
  30. Account Account::operator-(const_acc)
  31. {
  32.     return Account();
  33. }

  34. __ARITHMETIC_API_END
复制代码


main.cpp:
  1. #include "Arithmetic.h"

  2. int main(int argc, char *argv[])
  3. {
  4.         api::Account* current = new api::Account;

  5.         std::wcout << L"--- 欢迎使用口算APP ---" << std::endl;
  6.         std::wcout << L"-- - 请输入操作-- - "  << std::endl;
  7.         std::wcout << L"1. XXX" << std::endl;

  8.         return 0;
  9. }
复制代码


编译器返回:
  1. 已启动生成...
  2. 1>------ 已启动生成: 项目: 口算APP - Console, 配置: Debug x64 ------
  3. 1>Arithmetic.cpp
  4. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,39): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  5. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,27): error C2146: 语法错误: 缺少“;”(在标识符“account_type”的前面)
  6. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  7. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,27): error C2146: 语法错误: 缺少“;”(在标识符“const_acc”的前面)
  8. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,36): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  9. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,59): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  10. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  11. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,27): error C2146: 语法错误: 缺少“;”(在标识符“const_subacc”的前面)
  12. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,39): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  13. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,62): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  14. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(28,6): error C2371: “api::AccountType”: 重定义;不同的基类型
  15. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,15): message : 参见“api::AccountType”的声明
  16. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(50,7): error C2371: “api::Account”: 重定义;不同的基类型
  17. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,15): message : 参见“api::Account”的声明
  18. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(84,7): error C2371: “api::SubAccount”: 重定义;不同的基类型
  19. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,15): message : 参见“api::SubAccount”的声明
  20. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(5,7): error C2825: 'api::Account': 当后面跟“::”时必须为类或命名空间
  21. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(5,16): error C2510: “Account”:“::”的左边必须是类/结构/联合
  22. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(10,6): error C2825: 'api::Account': 当后面跟“::”时必须为类或命名空间
  23. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(10,15): error C2510: “Account”:“::”的左边必须是类/结构/联合
  24. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(15,7): error C2825: 'api::Account': 当后面跟“::”时必须为类或命名空间
  25. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(15,16): error C2510: “Account”:“::”的左边必须是类/结构/联合
  26. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(20,6): error C2825: 'api::Account': 当后面跟“::”时必须为类或命名空间
  27. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(20,15): error C2510: “Account”:“::”的左边必须是类/结构/联合
  28. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(26,47): error C2061: 语法错误: 标识符“const_acc”
  29. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(26,16): error C2805: 二进制“operator <<”的参数太少
  30. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(32,47): error C2061: 语法错误: 标识符“const_acc”
  31. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(32,16): error C2805: 二进制“operator >>”的参数太少
  32. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(38,9): error C2825: 'api::Account': 当后面跟“::”时必须为类或命名空间
  33. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(38,18): error C2510: “Account”:“::”的左边必须是类/结构/联合
  34. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.cpp(39,1): error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
  35. 1>main.cpp
  36. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,39): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  37. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,27): error C2146: 语法错误: 缺少“;”(在标识符“account_type”的前面)
  38. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  39. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,27): error C2146: 语法错误: 缺少“;”(在标识符“const_acc”的前面)
  40. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,36): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  41. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,59): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  42. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  43. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,27): error C2146: 语法错误: 缺少“;”(在标识符“const_subacc”的前面)
  44. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,39): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  45. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,62): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
  46. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(28,6): error C2371: “api::AccountType”: 重定义;不同的基类型
  47. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(18,15): message : 参见“api::AccountType”的声明
  48. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(50,7): error C2371: “api::Account”: 重定义;不同的基类型
  49. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(19,15): message : 参见“api::Account”的声明
  50. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(84,7): error C2371: “api::SubAccount”: 重定义;不同的基类型
  51. 1>F:\私人\C++\Program\Console\口算APP - Console\Arithmetic.h(20,15): message : 参见“api::SubAccount”的声明
  52. 1>F:\私人\C++\Program\Console\口算APP - Console\main.cpp(5,26): error C2737: “api::Account”: 必须初始化 const 对象
  53. 1>正在生成代码...
  54. 1>已完成生成项目“口算APP - Console.vcxproj”的操作 - 失败。
  55. ========== 生成: 0 成功,1 失败,0 最新,0 已跳过 ==========
  56. ========= 生成 开始于 10:35 AM,并花费了 01.050 秒 ==========
复制代码

求助!
最佳答案
2023-4-1 10:36:21
主要是 Arithmetic.h 的问题,因为 typedef 的位置不对,所以就报错了:
  1. #pragma once

  2. #ifndef _ARITHMETIC_
  3. #define _ARITHMETIC_

  4. #define __ARITHMETIC_API_BEGIN namespace api {
  5. #define __ARITHMETIC_API_END   }
  6. #define _API                   api::

  7. #include <iostream>
  8. #include <string>
  9. #include <cstdlib>
  10. #include <stack>

  11. __ARITHMETIC_API_BEGIN

  12. typedef       void*       idc_pointer;

  13. enum EventType
  14. {
  15.         SystemEvent, // 系统消息
  16.         OtherEvent,  // 其他消息
  17. };

  18. enum AccountType
  19. {
  20.         SYSTEM,
  21.         ADMINISTRATOR,
  22.         STANDARD_USER,
  23.         BAN_USER,
  24.         ALL_BAN_USER,
  25. };
  26. typedef       AccountType account_type;

  27. struct Event
  28. {
  29.         EventType    event_type;
  30.         std::wstring event;
  31. };

  32. class IDC
  33. {
  34. private:
  35.         const char id[18];
  36.         const char    key;
  37. };

  38. class Account
  39. {
  40. public:
  41.         Account(void)  = default;
  42.         ~Account(void) = default;

  43.         std::wstring name; // 昵称
  44.         
  45.         static void*     operator new(size_t size);      //     重  载     //
  46.         static void   operator delete(void* pointer);   //     运算符     //
  47.         static void*   operator new[](size_t size);  //     函  数     //
  48.         static void operator delete[](void* pointer); // new and delete //

  49.         template <idc_pointer* idc>
  50.         Account operator +(Account acc);

  51.         template <idc_pointer* idc>
  52.         Account operator -(Account acc);

  53. protected:
  54.         long long int uid; // 用户 ID

  55. private:
  56.         unsigned long long int score = 0;
  57.         unsigned long long int exp   = 0;

  58.         std::stack <Event> event_stack;

  59.         account_type acc_type = STANDARD_USER;

  60.         friend std::wostream& operator <<(std::wostream& wos, Account acc);
  61.         friend std::wistream& operator >>(std::wistream& wis, Account acc);
  62. };
  63. typedef const Account     const_acc,    *const_acc_pointer;

  64. class SubAccount : public Account
  65. {
  66. public:
  67.         SubAccount(void)  = default;
  68.         ~SubAccount(void) = default;

  69. private:
  70.         unsigned long long int sub_uid; // 子用户 ID
  71. };

  72. typedef const SubAccount  const_subacc, *const_subacc_pointer;
  73. __ARITHMETIC_API_END

  74. #endif
复制代码

另外,你的 new,delete 等运算没有写呢

最佳答案

查看完整内容

主要是 Arithmetic.h 的问题,因为 typedef 的位置不对,所以就报错了: 另外,你的 new,delete 等运算没有写呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-1 10:36:21 | 显示全部楼层    本楼为最佳答案   
主要是 Arithmetic.h 的问题,因为 typedef 的位置不对,所以就报错了:
  1. #pragma once

  2. #ifndef _ARITHMETIC_
  3. #define _ARITHMETIC_

  4. #define __ARITHMETIC_API_BEGIN namespace api {
  5. #define __ARITHMETIC_API_END   }
  6. #define _API                   api::

  7. #include <iostream>
  8. #include <string>
  9. #include <cstdlib>
  10. #include <stack>

  11. __ARITHMETIC_API_BEGIN

  12. typedef       void*       idc_pointer;

  13. enum EventType
  14. {
  15.         SystemEvent, // 系统消息
  16.         OtherEvent,  // 其他消息
  17. };

  18. enum AccountType
  19. {
  20.         SYSTEM,
  21.         ADMINISTRATOR,
  22.         STANDARD_USER,
  23.         BAN_USER,
  24.         ALL_BAN_USER,
  25. };
  26. typedef       AccountType account_type;

  27. struct Event
  28. {
  29.         EventType    event_type;
  30.         std::wstring event;
  31. };

  32. class IDC
  33. {
  34. private:
  35.         const char id[18];
  36.         const char    key;
  37. };

  38. class Account
  39. {
  40. public:
  41.         Account(void)  = default;
  42.         ~Account(void) = default;

  43.         std::wstring name; // 昵称
  44.         
  45.         static void*     operator new(size_t size);      //     重  载     //
  46.         static void   operator delete(void* pointer);   //     运算符     //
  47.         static void*   operator new[](size_t size);  //     函  数     //
  48.         static void operator delete[](void* pointer); // new and delete //

  49.         template <idc_pointer* idc>
  50.         Account operator +(Account acc);

  51.         template <idc_pointer* idc>
  52.         Account operator -(Account acc);

  53. protected:
  54.         long long int uid; // 用户 ID

  55. private:
  56.         unsigned long long int score = 0;
  57.         unsigned long long int exp   = 0;

  58.         std::stack <Event> event_stack;

  59.         account_type acc_type = STANDARD_USER;

  60.         friend std::wostream& operator <<(std::wostream& wos, Account acc);
  61.         friend std::wistream& operator >>(std::wistream& wis, Account acc);
  62. };
  63. typedef const Account     const_acc,    *const_acc_pointer;

  64. class SubAccount : public Account
  65. {
  66. public:
  67.         SubAccount(void)  = default;
  68.         ~SubAccount(void) = default;

  69. private:
  70.         unsigned long long int sub_uid; // 子用户 ID
  71. };

  72. typedef const SubAccount  const_subacc, *const_subacc_pointer;
  73. __ARITHMETIC_API_END

  74. #endif
复制代码

另外,你的 new,delete 等运算没有写呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-4-1 10:39:41 | 显示全部楼层
42个错误!42.zip?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-1 10:44:19 | 显示全部楼层

                               
登录/注册后可看大图

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-1 10:47:12 | 显示全部楼层

666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-1 11:17:20 | 显示全部楼层
  1. static void* Account::operator new(size_t size)
  2. {
  3.     return malloc(size);
  4. }

  5. static void Account::operator delete(void* pointer)
  6. {
  7.     free(pointer);
  8. }

  9. static void* Account::operator new[](size_t size)
  10. {
  11.     return malloc(size);
  12. }

  13. static void Account::operator delete[](void* pointer)
  14. {
  15.     free(pointer);
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-1 11:29:48 | 显示全部楼层
这份代码编译出现了多个错误。

首先,Arithmetic.h 中的 AccountType 枚举和 Account、SubAccount 类的声明出现了重定义。这可能是因为头文件被包含了多次导致的。可以使用预处理器指令 #ifndef 和 #define 来避免头文件的重复包含。

另外,在 main.cpp 中,创建 current 对象时没有对其进行初始化,而该对象是一个 const 对象,所以必须在创建时进行初始化。

针对以上问题,推荐修改后的代码如下:



  1. Arithmetic.h
  2. ```c++
  3. #ifndef ARITHMETIC_H_
  4. #define ARITHMETIC_H_

  5. namespace api {

  6.     // 账户类型枚举
  7.     enum AccountType {
  8.         VIP = 0,
  9.         NORMAL = 1
  10.     };

  11.     // 账户基类
  12.     class Account {
  13.     public:
  14.         // 构造函数
  15.         Account(AccountType account_type, double balance) :
  16.             account_type_(account_type), balance_(balance) {}

  17.         // 存款虚函数
  18.         virtual bool deposit(double amount) = 0;

  19.         // 取款虚函数
  20.         virtual bool withdraw(double amount) = 0;

  21.         // 获取账户类型的方法
  22.         AccountType get_account_type() const {
  23.             return account_type_;
  24.         }

  25.         // 获取账户余额的方法
  26.         double get_balance() const {
  27.             return balance_;
  28.         }

  29.     protected:
  30.         AccountType account_type_;  // 账户类型
  31.         double balance_;  // 账户余额
  32.     };

  33.     // 子账户类,继承自账户基类
  34.     class SubAccount : public Account {
  35.     public:
  36.         // 构造函数,需传入父账户指针
  37.         SubAccount(AccountType account_type, Account* parent_acc) :
  38.             Account(account_type, 0), parent_account_(parent_acc) {}

  39.         // 存款实现,同时向父账户存款
  40.         bool deposit(double amount) override;

  41.         // 取款实现,同时向父账户取款
  42.         bool withdraw(double amount) override;

  43.     private:
  44.         Account* parent_account_;  // 父账户指针
  45.     };
  46. }

  47. #endif  // ARITHMETIC_H_
  48. ```

  49. Arithmetic.cpp
  50. ```c++
  51. #include "Arithmetic.h"

  52. namespace api {

  53.     bool SubAccount::deposit(double amount) {
  54.         balance_ += amount;
  55.         parent_account_->deposit(amount);  // 向父账户存款
  56.         return true;
  57.     }

  58.     bool SubAccount::withdraw(double amount) {
  59.         if (balance_ >= amount) {
  60.             balance_ -= amount;
  61.             parent_account_->withdraw(amount);  // 向父账户取款
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66. }
  67. ```

  68. main.cpp
  69. ```c++
  70. #include <iostream>
  71. #include "Arithmetic.h"

  72. int main(int argc, char *argv[]) {
  73.     api::Account* current = new api::Account(api::AccountType::NORMAL, 0.0);

  74.     std::wcout << L"--- 欢迎使用口算APP ---" << std::endl;
  75.     std::wcout << L"-- - 请输入操作-- - "  << std::endl;
  76.     std::wcout << L"1. XXX" << std::endl;

  77.     delete current;   // 记得释放内存

  78.     return 0;
  79. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 10:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表