5 DERECHA

LNK 2005 본문

Hard nut to crack/Link Error

LNK 2005

kay30 2022. 12. 16. 00:07

[Error]

LNK2005 : "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl Advent::input(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?input@Advent@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V23@@Z)이(가) Day02_1.obj에 이미 정의되어 있습니다. AdventOfCode2022 C:\Users\USER\Desktop\kay\study\AdventOfCode2022\AdventOfCode2022\AdventOfCode2022\Day03_1.obj 1

[Situation]

  When I call the function input(), compiler said that I already definded the function in another cpp file.

#ifndef __ADVENT_H__
#define __ADVENT_H__
#endif

#include <iostream>
#include <stdio.h>
#include <unordered_map>
#include <string> // getline header
#include <fstream> // ifstream header
#include <sstream> // stringstream header

#define DEBUG_ASSERT cout << "debug" <<endl

using namespace std;

namespace Advent
{
	string input(string filename)
	{
		// setbuf(stdout, NULL);
		// freopen("input02_1.txt", "r", stdin);
		ifstream file(filename);
		if (file.fail())
		{
			DEBUG_ASSERT;
		}
		stringstream buf;
		buf << file.rdbuf();
		string contents = buf.str();
		return contents;
	}

	void solution03();
	// empty;
}
#ifndef _ADVENT_OF_CODE_2022_
#define _ADVENT_OF_CODE_2022_
#endif

#include "Advent.h"

using namespace std;

namespace Advent
{
	enum class Shape { Rock, Paper, Scissors };
	enum class Result { Loss, Draw, Win };
	int result[3][3] = { {3,0,6}, {6,3,0}, {0,6,3} };
	int result2[3][3] = { {3,4,8}, {1,5,9}, {2,6,7} };

	void Solution()
	{
		string contents = input("input02_1.txt");
		int score = 0;

		for (register int i = 0; i < contents.size(); i += 4)
		{
			int opponentShape = static_cast<int>(contents[i] - 'A');
			//int playerShape = static_cast<int>(contents[i + 2] - 'X');
			int playerResult = static_cast<int>(contents[i + 2] - 'X');

			// score += playerShape + 1 + result[playerShape][opponentShape];
			score += result2[opponentShape][playerResult];
			printf("%d\n", score);
		}
	}
}

/*
unorderd_map
- map 보다 더 빠른 탐색 가능한 자료구조
- Pros : Hash Table로 time complexity O(1)
- Cons : key가 유사한 데이터 많으면 충돌로 성능 떨어짐 + memory
*/
#ifndef __ADVENTOF_CODE_2022__
#define __ADVENTOF_CODE_2022__
#endif

#include "Advent.h"

using namespace std;

namespace Advent
{

	int local_hash[100];

	void localHashClear()
	{
		for (int i = 0; i < 100; i++)
		{
			local_hash[i] = 0;
		}
	}

	void solution03()
	{
		string contents = Advent::input("input03.txt");

		localHashClear();
		int num;

		for (register int i = 0; i < contents.size();)
		{
			register int j = i;
			while (contents[j] != '\n')
			{
				local_hash[contents[j++] - 'A']++;
			}
			for (int k = 0; k < 100; k++)
			{
				if (local_hash[k] == 2)	num = k;
				local_hash[k] = 0;
			}
			if (num > 26) num -= 31;
			else num += 1;
			printf("%d\n", num);
			i = j;
		}
	}
}
#ifndef __COREMAIN__
#define __COREMAIN__
#endif

#include "Advent.h"

using namespace Advent;

int main() {
	printf("Which problem do you want to solve ? ");

	int num;
	scanf("%d", num);

	solution03();
	//af_solution[num].();
}

 

[Case]

1) Wrong way to declare global variable - my case;;

2) Declare same name's function in another cpp file

3) Didn't make any header file and compile

4) #include "~.cpp"

 

[Solution]

My way : put "static "