调用静态方法,出现错误:LNK2019

peu*_*art 3 c++ static

我在制作仅具有静态方法的字符串实用程序类时遇到了一些麻烦。每当我使用调用类在字符串实用程序类中使用静态方法时,它都会编译并显示 LNK 错误,2019。任何帮助将不胜感激。.h 如下,

#pragma once
#include <string>
#include "stdafx.h"
#include <iostream>
using namespace std;
static class StringUtil
{
public:
    static string Reverse(string);
   // bool Palindrome(string);
   // string PigLatin(string);
   // string ShortHand(string); 
private:
   // string CleanUp(string);
};
Run Code Online (Sandbox Code Playgroud)

.cpp 文件如下,

   #include "StdAfx.h"
   #include "StringUtil.h"
   #include <iostream>

static string Reverse(string phrase)
{
    string nphrase = "";
    for(int i = phrase.length() - 1; i > 0; i--)
    {
        nphrase += phrase[i];
    }
    return nphrase;
}
Run Code Online (Sandbox Code Playgroud)

下面是调用类。

#include "stdafx.h"
#include <iostream>
#include "StringUtil.h"

void main() 
{
    cout << "Reversed String: " << StringUtil::Reverse("I like computers!");
}
Run Code Online (Sandbox Code Playgroud)

当它运行时,它显示

错误 5 错误 LNK2019:无法解析的外部符号“public:静态类 std::basic_string,class std::allocator > __cdecl StringUtil::Reverse(class std::basic_string,class std::allocator >)”(?Reverse@StringUtil@ @SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V23@@Z) 在函数“void __cdecl a10_StringUtil(void)”(?a10_StringUtil) 中引用@@YAXXZ) H:\Visual Studio 2010\Projects\面向对象的 C++\面向对象的 C++\面向对象的 C++.obj 面向对象的 C++

错误 6 错误 LNK1120: 1 无法解析的外部 H:\Visual Studio 2010\Projects\Object Oriented C++\Debug\Object Oriented C++.exe 1 1 Object Oriented C++

我觉得这是一个很简单的问题,但我习惯用Java编程。我目前正在尝试自学如何用 C++ 编写代码,因此出现了我的问题。

Den*_*tov 5

首先,在 C++ 中我们没有静态类

#pragma once
#include <string>
#include "stdafx.h"
#include <iostream>
using namespace std;

class StringUtil
{
public:
    static string Reverse(string);
   // bool Palindrome(string);
   // string PigLatin(string);
   // string ShortHand(string); 
private:
   // string CleanUp(string);
};
Run Code Online (Sandbox Code Playgroud)

其次,您忘记了类名 StringUtil(所有者):

string StringUtil::Reverse(string phrase)
{
    string nphrase = "";
    for(int i = phrase.length() - 1; i >= 0; i--)
    {
        nphrase += phrase[i];
    }
    return nphrase;
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你 :)