0

こんにちは。私は Visual C++ から始めていますが、理解できないコンパイルの問題があります。

私が得るエラーは次のとおりです。

エラー LNK1120 外部リンクが未解決

エラー LNK2019

コードを貼り付けます:

C++TestingConsole.CPP

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

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
string res = StringUtils::GetProperSalute("Carlos").c_str();
cout << res;
return 0;
}

StringUtils.cpp

#include "StdAfx.h"
#include <stdio.h>
#include <ostream>
#include "StringUtils.h"
#include <string>
#include <sstream>
using namespace std;


static string GetProperSalute(string name)
{
return "Hello" + name;
}

ヘッダー: StringUtils.h

#pragma once
#include <string>
using namespace std;



class StringUtils
{

public:

static string GetProperSalute(string name);

};
4

1 に答える 1

2

クラス定義でメソッドを宣言し、static定義時にクラス名で修飾するだけです。

static string GetProperSalute(string name)
{
return "Hello" + name;
}

する必要があります

string StringUtils::GetProperSalute(string name)
{
return "Hello" + name;
}

その他の注意事項:

  • 削除しusing namespace std;ます。完全な資格を好む(例std::string
  • あなたのクラスStringUtilsは、より適しているようですnamespace(これは、コードにさらに変更を加えることを意味します)
  • string res = StringUtils::GetProperSalute("Carlos").c_str();役に立たない、あなたはただ行うことができます:string res = StringUtils::GetProperSalute("Carlos");
  • 文字列をconst値ではなく参照で渡します。std::string GetProperSalute(std::string const& name)
于 2012-10-03T14:24:25.197 に答える