简体   繁体   English

在 h 文件中找不到 function 定义

[英]function definition not found in h file

I don't really understand what Visual Studio 2019 wants from me (C language).我真的不明白 Visual Studio 2019 想要我做什么(C 语言)。 On one hand it doesn't throw me either error or warning, but on the other hand it marks me with a green squiggling the createCustomer decaration in my API file.一方面,它不会向我抛出错误或警告,但另一方面,它会在我的 API 文件中用绿色波浪线标记我的createCustomer声明。 I would like to encapsulate Customer and use ADT on Customer structure.我想封装客户并在客户结构上使用 ADT。

These are my source and header files:这些是我的源文件和 header 文件:

customer.h客户.h

#include <stdlib.h>

typedef struct Customer
{
    unsigned int customerId;
    const char* name;
    int numOrders;
}Customer, * CustomerPtr;

customer.c客户.c

#include "customer.h"

#define MAX_NO_OF_CUSTOMERS 10
static Customer objectPool[MAX_NO_OF_CUSTOMERS];
static unsigned int customersCount = 0;

CustomerPtr createCustomer(const char* name, size_t numOrders)
{
    CustomerPtr newCustomer_p = NULL;
    if(customersCount < MAX_NO_OF_CUSTOMERS)
    {
        newCustomer_p = objectPool + (customersCount++);
        newCustomer_p->name = name;
        newCustomer_p->numOrders = numOrders;
    }
}

customer_api.h customer_api.h

#include "customer.h"

CustomerPtr createCustomer(const char* name, int numOrders);

main.c main.c

#include <stdio.h>
#include "main.h"
#include "customer_api.h"

int main()
{
    CustomerPtr customer_p = createCustomer("Demo", 5);
    return 0;
}

So as I said, under customer_api.h the following CustomerPtr createCustomer(const char* name, int numOrders);正如我所说,在customer_api.h下,以下CustomerPtr createCustomer(const char* name, int numOrders); has squiggle under the function declaration createCustomer .在 function 声明createCustomer下有波浪线。 The program compiles and run successfully without errors/warnings.该程序编译并成功运行,没有错误/警告。

Maybe I'm not using correctly the API h file concept?也许我没有正确使用 API h 文件概念? I'm trying to keep the customer properties implementation hidden from external files, so in other files I just include customer_api.h anytime I need to approach the customer module.我试图使客户属性实现对外部文件隐藏,因此在其他文件中,只要我需要接近客户模块,我只需包含customer_api.h

On one hand it doesn't throw me either error or warning …一方面,它不会给我带来错误或警告……

You should enable all warnings in your project settings;您应该在项目设置中启用所有警告; see: Why should I always enable compiler warnings?请参阅:为什么我应该始终启用编译器警告? With warnings enabled, your code (or a version of it I pasted into my VS 2019 IDE) generates 5 warnings:启用警告后,您的代码(或我粘贴到 VS 2019 IDE 的代码版本)会生成 5 个警告:

warning C4255: 'main': no function prototype given: converting '()' to '(void)'警告 C4255:'main': no function 给出原型:将'()' 转换为'(void)'
warning C4189: 'customer_p': local variable is initialized but not referenced警告 C4189:“customer_p”:局部变量已初始化但未引用
warning C4028: formal parameter 2 different from declaration警告 C4028:形参 2 与声明不同
warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data警告 C4267:“=”:从“size_t”转换为“int”,可能丢失数据
warning C4716: 'createCustomer': must return a value警告 C4716:“createCustomer”:必须返回一个值

Now, #1, #2 and #4 can be put aside (for now);现在,#1、#2 和#4 可以放在一边(暂时); the big issues are #3 and #5.大问题是#3 和#5。

To address #3: You need to make the arguments in your function definition (in "customer.c") the same as in the prototype (in "customer_api.h");要解决#3:您需要使 function 定义(在“customer.c”中)中的 arguments 与原型中(在“customer_api.h”中)相同; presumably, as the numOrders member of your structure is an int , you should change the former to have an int second argument (this also addresses and removes warning #4):据推测,由于结构的numOrders成员是int ,您应该将前者更改为具有int第二个参数(这也解决并删除了警告#4):

To address #5: The createCustomer function must return what it is declared to return: a CustomerPtr object.要解决 #5: createCustomer function必须返回它声明返回的内容: CustomerPtr object。 The local variable, newCustomer_p , is the obvious candidate for this.局部变量newCustomer_p是明显的候选者。

Here is a 'fixed' version of that function:这是 function 的“固定”版本:

CustomerPtr createCustomer(const char* name, int numOrders)
{
    CustomerPtr newCustomer_p = NULL;
    if (customersCount < MAX_NO_OF_CUSTOMERS) {
        newCustomer_p = objectPool + (customersCount++);
        newCustomer_p->name = name;
        newCustomer_p->numOrders = numOrders;
    }
    return newCustomer_p;
}

This leaves warnings #1 and #2.这会留下警告 #1 和 #2。 To fix #1, add void as the argument list for main (the empty parentheses don't actually define a function in C).要修复 #1,添加void作为main的参数列表(空括号实际上并未在 C 中定义function)。 Presumably, you'll be doing something with the unused variable, at some point, so that will address #2.据推测,您将在某个时候对未使用的变量做一些事情,这样就可以解决#2。

int main(void) // Note the EXPLICIT void argument list; () just means "unspecified" in C
{
    CustomerPtr customer_p = createCustomer("Demo", 5);
    // Do something with "customer_p"
    (customer_p);
    return 0;
}

… but on the other hand it marks me with a green squiggling. …但另一方面,它用绿色的波浪线标记了我。

Hovering the mouse over the squiggles will show the problem: "Function definition for 'createCustomer' not found."将鼠标悬停在曲线上会显示问题:“未找到 'createCustomer' 的函数定义。” This is caused by the issue that generates warning #3;这是由生成警告 #3 的问题引起的; fixing that resolves the green squiggles.解决绿色曲线的修复。

To keep the implementation of the type Customer hidden from the client module you need to put the structure definition in the C file.要使客户模块的实现类型隐藏,您需要将结构定义放在 C 文件中。 Here is one way to do it.这是一种方法。 In the code below I call CustomerPtr simply Customer because I think it is cleaner.在下面的代码中,我将CustomerPtr简称为Customer ,因为我认为它更简洁。 For the sake of simplicity and consistency it's a good practice to use the same name of the customer implementation and header file as the customer data type.为了简单和一致,最好使用与客户实现和 header 文件相同的名称作为客户数据类型。 I also recommend using the prefix Customer_ for each function in Customer.h .我还建议对Customer.h Customer_ I have also made a few other adjustments.我还做了一些其他的调整。

Customer.h客户.h

#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <stdlib.h>

typedef struct CustomerDesc *Customer;

Customer Customer_Create(const char *name, size_t numOrders);

#endif

Customer.c客户.c

#include "Customer.h"

#define LEN(array) (sizeof (array) / sizeof (array)[0])

struct CustomerDesc {
    unsigned int customerId;
    const char *name;
    int numOrders;
};

static struct CustomerDesc objectPool[10];
static unsigned int customersCount = 0;

Customer Customer_Create(const char *name, size_t numOrders)
{
    Customer newCustomer = NULL;
    if (customersCount < LEN(objectPool)) {
        newCustomer = objectPool + customersCount;
        newCustomer->name = name;
        newCustomer->numOrders = numOrders;
        customersCount++;
    }
    return newCustomer;
}

main.c main.c

#include "Customer.h"

int main(void)
{
    Customer customer = Customer_Create("Demo", 5);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM