开发者

"function already has a body"

W开发者_StackOverflow中文版hat does this mean?

1>c:\users\vitali\documents\visual studio 2010\projects\salam\tools.cpp(107): error C2084: function 'bool readXMLInteger(xmlNodePtr,const char *,int &)' already has a body
1>c:\users\vitali\documents\visual studio 2010\projects\salam\tools.h(52) : see previous definition of 'readXMLInteger'

tools.cpp(107):

bool readXMLInteger(xmlNodePtr node, const char* tag, int32_t& value)
{
    char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
    if(nodeValue)
    {
        value = atoi(nodeValue);
        xmlFreeXOXL(nodeValue);
        return true;
    }

    return false;
}

tools.h(52)

bool readXMLInteger(xmlNodePtr node, const char* tag, int& value);


Did you use include guards in your original header file?

For example:

#ifndef _TOOLS_H_
#define _TOOLS_H_

... your header body is here ...

#endif

This blocks against re-defining in each cpp where it is included.


It means that at some point your actual code is being re-read into the compile stream, so it seems two attempts at defining (as opposed to declaring) the function.

Suspect something about the way you set up the preprocessor statements.


Perhaps you already found the solution, but for me rebuilding the solution fixed it.

I moved my implementation from the header file to the .cpp file and the .pch file already had this info. So, I had to rebuild to fix this error.


The following doesn't actually answer your question, but I had the same problem with a different cause. This answer is only for the record.

Some people have a very bad style of adding code to the header file, resulting in constructor declarations like cMyClass() {} which is already considered to be a definition and not just a declaration (yes, even if it's located in the header file)

Removing those definitions by changing them into actual declarations e.g. cMyClass(); will solve this particular kind of problem.


It means the function is implemented somewhere else in your code.


Also, check if you have made a copy of the file (.cxx or .cpp extension) in the same directory. So the function will get defined twice.
I was getting the error for static functions!


Listen, this is going to sound dumb, but for anyone else coming across this, make sure you didn't accidentally try and include the cpp file instead of the header (right click file, copy full path, paste, it happens...)

Kept looking over that file extension for a while


You are getting this error because the header file is called multiple times from other locations. Insert #pragma onceat the top of your header file. It is worth taking a look to your all references and find out duplicate calls. Or you have curly brockets (let say, empty ;)) in your header definition.


its true, i used the auto suggestion function on visual studio and it actually offered to include cpp file, i would have never thougt of..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜