1

我的项目中有许多可以工作的 .cpp 文件。但这会激怒 Xcode 或编译器。

它无法识别free()malloc()但这也是 C。有什么问题?

自由的

malloc

标头 ssdpmessage.h 如下所示:

#ifndef _SSDPMESSAGE_H
#define _SSDPMESSAGE_H

#include "ssdptools.h"
#include <vector>
#include <arpa/inet.h>
#include "ssdpdb.h"

class SSDPMessage{
public:
    SSDPMessage();
    virtual ~SSDPMessage();
    //What type of message can we handle 
    virtual SSDP_TYPE GetType()=0;
    //Get the message dignature implemented in this class
    virtual std::vector<SSDP_HTTP_HEADER*> GetHeaderSignature();
    //Can this class parse the message with this signature ?
    virtual u8 CanProcess(std::vector<SSDP_HTTP_HEADER*> msgheaders);
    //Process the message, return value: 
    //0 : processed
    //1 : not for me, search for another to process
    //<0 : message was for me but there is an error
    virtual int Process(struct sockaddr* sender, std::vector<SSDP_HTTP_HEADER*> msgheaders)=0;
    //ReInit all members
    virtual void ReInit()=0;
    virtual SSDPDB* GetDB();
    virtual void SetDB(SSDPDB* db);
private:
    std::vector<SSDP_HTTP_HEADER*> mHeaderSignature;
protected:
    int AddSignatureHeader(char* fieldname, char* fieldvalue);
    SSDPDB *mDB;
private:
    SSDPMessage(const SSDPMessage &src);
    SSDPMessage& operator= (const SSDPMessage &src);            
};


#endif //_SSDPMESSAGE_H

包含和受影响的代码ssdpmessage.cpp如下所示:

#include "ssdpmessage.h"

SSDPMessage::SSDPMessage():mDB(NULL){
}

SSDPMessage::~SSDPMessage(){
    std::vector<SSDP_HTTP_HEADER*>::iterator it;
    for(it=mHeaderSignature.begin(); it<mHeaderSignature.end(); it++){
        free(*it);
    }
    mHeaderSignature.clear();
}



int SSDPMessage::AddSignatureHeader(char* fieldname, char* fieldvalue){
    SSDP_HTTP_HEADER *thisHeader = (SSDP_HTTP_HEADER*)malloc(sizeof(SSDP_HTTP_HEADER));
    thisHeader->fieldname = (u8*)fieldname;
    thisHeader->fieldnamelen = strlen(fieldname);
    thisHeader->fieldvalue = (u8*)fieldvalue;
    thisHeader->fieldvaluelen = strlen(fieldvalue);
    mHeaderSignature.push_back(thisHeader);         
    return mHeaderSignature.size();
}

这是来自upnpx库的代码。它在库的演示项目中没有问题。

4

1 回答 1

2

malloc要求您包括cstdlib.

于 2013-08-03T18:43:58.853 回答