在头文件function.h中声明void doAddCommodity(commodityManger& cm); 在function.h中定义void doAddComodity(commodityManger& cm){ string name; double price,discount; int num; fflush(stdin); cout<<"Input name:"; getline(cin,name); cout<<"Input price,num,discount:"; cin>>price>>num>>discount; cm.addCommodity(Commodity(name,price,num,discount)); } 主程序#include <iostream> #include"commodity.h" #include"commoditymanger.h" #include"function.h" #include<bits/stdc++.h> using namespace std; int main(){ cout<<"welcome!\n"; char choice; commodityManger cm; cm.readData("d:\\commodity03.data"); while(true){ choice=menu(); if(choice=='0') break; switch(choice){ case '1': doAddCommodity(cm);为什么还会报错
时间: 2024-04-19 10:28:15 浏览: 110
根据您提供的信息,报错的原因可能是您没有在主程序中包含 "function.h" 头文件。请确保在主程序中添加以下行:
#include "function.h"
如果您已经包含了 "function.h" 头文件,那么请检查一下头文件中是否存在其他错误。另外,还需要确保 "commodityManger" 和 "Commodity" 类型的定义在头文件中正确且可用。
如果问题仍然存在,请提供完整的错误信息以便进一步排查问题。
相关问题
如何在C++中为Dev开发过程正确地编写头文件(.h)?
在C++中,为Dev开发过程正确地编写头文件(.h),通常用于声明公共接口,以便其他源文件可以访问它们。下面是基本步骤:
1. **命名规则**:头文件名应该反映它所包含内容的逻辑,通常是类名加上`.h`或`.hpp`后缀。例如,如果有一个名为`MyClass`的类,头文件命名为`MyClass.h`或`MyClass.hpp`。
2. **声明**:在头文件中,只包含函数原型(未实现的部分)和类的公共属性。对于函数,使用 `void function_name(parameters);` 的形式;对于类成员,使用 `public: access_specifier member_name;`。
3. **避免直接实现**:尽量不要在头文件中提供完整的函数或成员变量实现,以防止多次包含时的潜在问题(比如多次定义)。这通常在`.cpp` 文件中完成。
4. **包含控制**:使用 `#ifndef` 和 `#define` 宏来防止循环包含。如:
```cpp
#ifndef MyClass_H_
#define MyClass_H_
// 正文...
#endif // MyClass_H_
```
5. **包含依赖**:如果头文件包含了另一个库的头文件,确保使用 `#include` 包含。
6. **内联函数**:对于小型函数,考虑将其标记为 `inline`,以优化编译后的代码。
7. **注释和文档**:提供清晰的注释解释头文件的目的和使用的函数。
c++ 头文件pthread.h如何给一个带自定义参数的函数开启多线程
可以使用pthread_create函数创建一个新线程,同时将自定义参数传递给新线程中的函数。具体的步骤如下:
1. 定义一个自定义结构体,用于存储传递给新线程中的函数的参数。
```
typedef struct {
int arg1;
char* arg2;
} ThreadArgs;
```
2. 定义一个函数,该函数即为将被新线程执行的函数。
```
void* threadFunction(void* args) {
ThreadArgs* t_args = (ThreadArgs*) args;
int arg1 = t_args->arg1;
char* arg2 = t_args->arg2;
// 在这里执行线程的任务
pthread_exit(NULL);
}
```
3. 在主线程中创建新线程,并传递自定义参数。
```
ThreadArgs t_args;
t_args.arg1 = 123;
t_args.arg2 = "Hello World";
pthread_t thread;
int result = pthread_create(&thread, NULL, threadFunction, (void*) &t_args);
if (result != 0) {
printf("Error: Failed to create thread.\n");
exit(EXIT_FAILURE);
}
```
在上述代码中,pthread_create函数的第一个参数是指向新线程的标识符的指针,第二个参数是线程的属性,可以设置为NULL表示使用默认属性。第三个参数是指向新线程将要执行的函数的指针,第四个参数是传递给新线程函数的参数,需要强制转换为void*类型。在新线程中,需要使用pthread_exit函数来退出线程。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![h](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)