I keep getting these errors:
Validation.obj : error LNK2005: "bool __cdecl dotDashRules(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 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<char,struct std::char_traits<char>,class std::allocator<char> > 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;
}

@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.

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

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

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

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.

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

Edited 4 Years Ago by thines01: n/a

This article has been dead for over six months. Start a new discussion instead.