me.obj error LNK2005 : "class..." already defined in main.obj

I'm new to header files and namespaces so I've been tryed to work on them through websites that show me examples of that. So I made my own namespace through a header and cpp file but I keep getting this error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  /* Full Error: 

1>me.obj : error LNK2005: "bool __cdecl me::is_empty(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?is_empty@me@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in main.obj
1>me.obj : error LNK2005: "bool __cdecl me::is_even(int)" (?is_even@me@@YA_NH@Z) already defined in main.obj
1>me.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl me::remove_doubles(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?remove_doubles@me@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV23@@Z) already defined in main.obj
1>Debug\me.obj : warning LNK4042: object specified more than once; extras ignored
1>C:\Users\aiden\source\repos\CPPNameSpace\Debug\CPPNameSpace.exe : fatal error LNK1169: one or more multiply defined symbols found

*/

//main.cpp

#include <iostream>
#include "me.h";

int main() {
	std::string str = "hello";
	std::cout << me::remove_doubles(str);
	return 0;
}

//me.h

#ifndef ME_H
#define ME_H

#pragma once;

#include <string>
#include <vector>
#include <sstream>

namespace me {
	bool is_empty(std::string str);
	bool is_even(int num);

	std::string remove_doubles(std::string& str);
}

#endif

//me.cpp

#include "me.h";

bool me::is_empty(std::string str) {
	return str == "";
}

bool me::is_even(int num) {
	return (num % 2) ? false : true;
}

std::string me::remove_doubles(std::string& str) {
	std::stringstream ss;

	for (char c : str) {
		ss << c << c;
	}

	str = ss.str();
	return str;
}


Any help please?

Edit:
IDE: Visual Studio 2019

Edit #2:
Had to remove the code in main() and recompile then re add the code. Must be a bug?
Last edited on
try a clean build and provide the build command
Majeek wrote:
Had to remove the code in main() and recompile then re add the code. Must be a bug?

It’s not a bug; you did something wrong.

Don’t fall into the trap of blaming the tools when something goes wrong.
Instead, figure out what you did wrong, and modify your behavior.

MSVC is very smart, and tries to reduce the amount of code recompilation needed. When you move stuff around without performing an appropriate cleanup, you will fool the compiler and get errors like this.

A simple rebuild all would have fixed it.


Also, you do not need a semicolon after an #include statement.

Hope this helps.
Topic archived. No new replies allowed.