Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;上课?_C++_Static_Static Functions - Fatal编程技术网

C++ 如何在C++;上课?

C++ 如何在C++;上课?,c++,static,static-functions,C++,Static,Static Functions,我有一个类,其头文件定义为: namespace mip { class CustomStatic { public: static const char* GetVersion(); }; } #include "CustomStatic.h" namespace mip { static const char* GetVersion() { return "hello"; } } 类文件定义为: n

我有一个类,其头文件定义为:

namespace mip {
    class CustomStatic {
        public:
            static const char* GetVersion();
    };
}
#include "CustomStatic.h"

namespace mip {
    static const char* GetVersion() {
        return "hello";
    }
}
类文件定义为:

namespace mip {
    class CustomStatic {
        public:
            static const char* GetVersion();
    };
}
#include "CustomStatic.h"

namespace mip {
    static const char* GetVersion() {
        return "hello";
    }
}
我正在从我的主类访问这个静态函数

#include "CustomStatic.h"

#include <iostream>

using std::cout;
using mip::CustomStatic;

int main() {
    const char *msg = mip::CustomStatic::GetVersion();
    cout << "Version " << msg << "\n";
}
我得到的错误如下:

架构x86_64的未定义符号:
“mip::CustomStatic::GetVersion()”,引用自: _MainApp-feb286中的main.o ld:未找到架构x86_64的符号clang:错误:链接器命令失败,退出代码为 1(使用-v查看调用)


您的静态函数未在cpp文件中正确实现

你需要像这样做

//.h
namespace mip
{
    class CustomStatic
    {
         public:
            static const char* GetVersion();
    };
}


//.cpp -> note that no static keyword is required...
namespace mip
{
    const char* CustomStatic::GetVersion()
    {
        return "hello";
    }
}

//use
int main(int argc, char *argv[])
{
    const char* msg{mip::CustomStatic::GetVersion()};
    cout << "Version " << msg << "\n";
}
/.h
命名空间mip
{
类CustomStatic
{
公众:
静态常量char*GetVersion();
};
}
//.cpp->请注意,不需要静态关键字。。。
命名空间mip
{
常量字符*CustomStatic::GetVersion()
{
回复“你好”;
}
}
//使用
int main(int argc,char*argv[])
{
const char*msg{mip::CustomStatic::GetVersion()};

无法将其定义为
const char*CustomStatic::GetVersion()<代码> >请阅读一些C++入门书。@宋元耀不<代码> static <代码>需要在此声明函数<代码> MIP::Cudistatic::GETVISTION/CODE >,但定义<代码> MIP::GETVIPux。