在头文件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 18:28:15 浏览: 9
根据您提供的信息,报错的原因可能是您没有在主程序中包含 "function.h" 头文件。请确保在主程序中添加以下行:
#include "function.h"
如果您已经包含了 "function.h" 头文件,那么请检查一下头文件中是否存在其他错误。另外,还需要确保 "commodityManger" 和 "Commodity" 类型的定义在头文件中正确且可用。
如果问题仍然存在,请提供完整的错误信息以便进一步排查问题。
相关问题
stm32的.c文件怎么在.h文件中定义
在头文件(.h)中可以使用函数声明来引用.c文件中的函数,并且需要在链接时将两个文件链接起来。以下是一个简单的例子:
假设我们有一个.c文件名为 "example.c",其中定义了一个名为 "example_function()" 的函数,我们想在一个头文件中引用这个函数。
- 在头文件(.h)中声明函数:
```
#ifndef EXAMPLE_H
#define EXAMPLE_H
void example_function();
#endif
```
- 在.c文件中实现函数:
```
#include "example.h"
void example_function(){
// Function implementation here
}
```
这样,在其他文件中包含 "example.h" 头文件时就可以使用 "example_function()" 函数了。同时,在编译和链接时,需要将 "example.c" 文件一同编译并链接到程序中。
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函数来退出线程。