1. ホーム

collect2.exe: エラー: ld が 1 の終了ステータスを返しました。

2022-02-26 19:24:25
<パス

collect2.exe: error: ld returned 1 exit statusというエラーでプログラムが実行されます。

c:/mingw/bin/... /lib/gcc/mingw32/9.2.0/... /... /... /... /mingw32/bin/ld.exe: C:\Temp\ccnQ8uo7.o:ex2.2.cpp:(.text+0xbb): undefined reference to `displayMessage(std::__cxx11::basic_string< char, std::char_traits<char>, std::allocator<char> > const&, std::vector<int, std::allocator<int> > const& amp;)'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.6s with exit code 1]

#include 

#include 


using namespace std;

const int maxSize = 1024;
void displayMessage(const string &msg, const vector<int> &vec);
const vector<int>* Pentagonal(int size);

int main(int argc, char const *argv[])
{
	int pos;

	cout << "Please enter a positon: ";
	cin >> pos;

	if (pos <= 0 || pos > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return -1;
	}
	
	displayMessage("Pentagonal:", *(Pentagonal(pos)));

	return 0;
}

template <typename elemType>
void displayMessage(const string &msg, const vector<elemType> &vec)
{
	cout << msg;
	for (int i = 0; i < vec.size(); ++i)
	{
		elemType t = vec[i];
		cout << t << ' ';
	}
}

const vector<int>* Pentagonal(int size)
{
	static vector<int> elems;
	const string msg("Requested size is not supported ");


	if (size <= 0 || size > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return 0;
	}

	for (int i = elems.size(); i < size; ++i)
		elems.push_back((i + 1) * (3 * i + 2) / 2);
	
	return &elems;
}


#include 

#include 


using namespace std;

const int maxSize = 1024;

template <typename elemType>
void displayMessage(const string &msg, const vector<elemType> &vec)
{
	cout << msg;
	for (int i = 0; i < vec.size(); ++i)
	{
		elemType t = vec[i];
		cout << t << ' ';
	}
}

const vector<int>* Pentagonal(int size)
{
	static vector<int> elems;

	if (size <= 0 || size > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return 0;
	}

	for (int i = elems.size(); i < size; ++i)
	{
		elems.push_back((i + 1) * (3 * i + 2) / 2);
	}
	return &elems;
}


int main(int argc, char const *argv[])
{
	int pos;

	cout << "Please enter a positon: ";
	cin >> pos;

	if (pos <= 0 || pos > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return -1;
	}
	
	displayMessage("Pentagonal:", *(Pentagonal(pos)));

	return 0;
}


オリジナルブログからの参照 とか、関数の宣言と実装の間に問題があるはずで、正確な理由を理解せず、関数の実装を前倒しにして、エラー報告をしなくなったとか、そういうことです。

#include 

#include 


using namespace std;

const int maxSize = 1024;

template <typename elemType>
void displayMessage(const string &msg, const vector<elemType> &vec)
{
	cout << msg;
	for (int i = 0; i < vec.size(); ++i)
	{
		elemType t = vec[i];
		cout << t << ' ';
	}
}

const vector<int>* Pentagonal(int size)
{
	static vector<int> elems;

	if (size <= 0 || size > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return 0;
	}

	for (int i = elems.size(); i < size; ++i)
	{
		elems.push_back((i + 1) * (3 * i + 2) / 2);
	}
	return &elems;
}


int main(int argc, char const *argv[])
{
	int pos;

	cout << "Please enter a positon: ";
	cin >> pos;

	if (pos <= 0 || pos > maxSize)
	{
		cout << "Requested size is not supported!" << endl;
		return -1;
	}
	
	displayMessage("Pentagonal:", *(Pentagonal(pos)));

	return 0;
}


declare first, call laterのパターンをどう解決するかはまだ考えていません。