“function already has a body”

你。 提交于 2020-01-14 09:11:15

问题


What 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);

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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!



来源:https://stackoverflow.com/questions/4988593/function-already-has-a-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!