954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error LNK2005: "bool __cdecl"

I keep getting these errors:
Validation.obj : error LNK2005: "bool __cdecl dotDashRules(class std::basic_string,class std::allocator > const &)" (?dotDashRules@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in main.obj

Validation.obj : error LNK2005: "bool __cdecl IsValid(class std::basic_string,class std::allocator > const &)" (?IsValid@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in main.obj

I can't get rid of them. This program is an email validation program, btw.

main.cpp

// Homework 1

#include <iostream>
#include <fstream>
#include <string>
#include "Validation.cpp"

using namespace std;

bool IsValid(const string& emailAddress);

void main()
{
	//Input file to read emails from
	ifstream in("Email.txt");

	//Output file to write 0s and 1s to
	ofstream out("Result.txt");


	//0. While we are not at the end of file...
	while(!in.eof())
	{
		string emailAddress;
	//1. Read email from Email.txt, capturing an entire line;
		getline(in, emailAddress);

	//2. Validate it
		bool isValid = IsValid(emailAddress);

	//3. Write result to Result.txt
		out << isValid << endl;
	}
	in.close();
	out.close();


	system("pause");
}

Validation.cpp

#include <iostream>
#include <string>
#include <fstream>
#pragma once
#define MAX_EMAIL_SIZE 320
#define MAX_LOCALPART_SIZE 64
#define MAX_DOMAIN_SIZE 255
#define VALID_LOCALPART_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-."
#define DOT '.'
#define AT '@'
#define DOT_DOT ".."
#define DOT_DASH ".-"
#define DASH_DOT "-."
#define WHITE_SPACE ' '

using namespace std;

//Email validation function
bool dotDashRules(const string& name)
{
	int checkLocal = name.find(DOT_DOT);
	if(checkLocal != string::npos)
		return false;
	checkLocal = name.find(DOT_DASH);
	if(checkLocal != string::npos)
		return false;
	checkLocal = name.find(DASH_DOT);
	if(checkLocal != string::npos)
		return false;
	checkLocal = name.find(DOT);
	if(checkLocal == 0 || checkLocal == name.length())
		return false;
	checkLocal = name.find(WHITE_SPACE);
	if(checkLocal != string::npos)
		return false;
}

bool IsValid(const string& emailAddress)
{
	//1. Check emailAddress size
	if(emailAddress.length() > MAX_EMAIL_SIZE)
		return false;

	//2. Find '@' symbol
	int atPosition = emailAddress.find(AT);
	if(atPosition == string::npos)
		return false;

	//3. Extract localPart and domain
	string localPart, domain, domainString;
	int dot, domainDot;
	localPart = emailAddress.substr(0, atPosition-1);
	dot = emailAddress.rfind(".");
	domainString = emailAddress.substr(atPosition+1, (dot-1));
	domainDot = domainString.rfind(".");
	if(domainDot == string::npos)
		domain = emailAddress.substr(domainDot+1, dot-1);
	else
		domain = emailAddress.substr(atPosition+1, (dot-1));

	//CHECK localPart
	//4. Check localPart size
	if(localPart.length() > MAX_LOCALPART_SIZE)
		return false;

	//5. Check for valid characters
	int check = localPart.find_first_not_of(VALID_LOCALPART_CHARS);
	if(check != string::npos)
		return false;

	//6. Check localPart for '.' and '-' rule
	dotDashRules(localPart);

	//CHECK domain
	//7. Check size of domain
	if(domain.length() > MAX_DOMAIN_SIZE)
		return false;

	//8. Find TLD; start at end and look for '.'
	int topLevDo = emailAddress.rfind(DOT);

	//9. Extract TLD
	string topLevelDomain = emailAddress.substr(topLevDo+1);

	//10. Validate TLD
	ifstream infile("TLD.txt");
	string listDomain = "nothing";
	while(!infile.eof() && listDomain != topLevelDomain)
			infile >> listDomain;
	infile.close();
	if(listDomain != topLevelDomain)
		return false;

	//11. Extract subdomain
	string subdomain = emailAddress.substr(atPosition+1, (topLevDo-1));

	//12. Check subdomain for valid characters
	int subdomainCheck = subdomain.find_first_not_of(VALID_LOCALPART_CHARS);
	if(subdomainCheck != string::npos)
		return false;

	//13. Check subdomain for '.' and '-' rule
	if(!dotDashRules(subdomain))
		return false;
}
xtinab
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Line 6 should include a.h file, not a .cpp file

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

@thines01: considering how the compiler interpets it, it should work, but you are 100% correct that this way shouldn't be used.

IMO, just delete line 10.

jaskij
Junior Poster
105 posts since Oct 2011
Reputation Points: 55
Solved Threads: 18
 

I have tried deleting line 10 and it still does not compile.

xtinab
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
Line 6 should include a.h file, not a .cpp file

Even when I do that it still won't compile.

xtinab
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Might be something VS specific, compiled just fine with g++ even without deleting line 10 ;P
Oh well, -Wall shows some warning, but nothing much.

Also, include cstdlib - you don't provide the header for the system() function. And you don't need that system("pause") anyways, since you don't output anything to the standard output, only to files.

jaskij
Junior Poster
105 posts since Oct 2011
Reputation Points: 55
Solved Threads: 18
 

When you delete 10 or replace it with a .h file, you get a different list of errors, right?
What is the first error?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: