already defined in functions.obj

Greetings all and thank you in advance for any help you can provide. I am relatively new to C++. I took a beginners class in college but that was about 6 years ago and have not touched it since then. In recent times I have taken a renewed interest in C++ and have dreamed up a few smaller projects to help me learn and relearn C++. The project I am working with right now should accept a character from the user and return to the user the ASCII equivalent from the ASCII table. The code compiles fine but has linking problems. I have searched and searched but could not find any solutions to my problems. The error I recieve is the one posted in the subject of this message. Perhaps there is an obvious answer, but the problem has eluded me thus far.

The following is what I get when I go to rebuild:
"1>------ Rebuild All started: Project: translator, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'translator', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>functions.cpp
1>translator.cpp
1>Generating Code...
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>translator.obj : error LNK2005: "int __cdecl tascii(char * const)" (?tascii@@YAHQAD@Z) already defined in functions.obj
1>C:\Documents and Settings\George\My Documents\Visual Studio 2008\Projects\translator\Debug\translator.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\George\My Documents\Visual Studio 2008\Projects\translator\translator\Debug\BuildLog.htm"
1>translator - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped =========="

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
The end result of this application should accept a character and convert it to its ASCII counterpart.
*/

/* Standard Includes */
#include "stdafx.h"		//Here by default, not sure why
#include <ctype.h>		//For using toupper
#include <iostream>		//Used for cin and cout
#include <string>		//Used for string access

/* Custom Includes */
/* This include file has been excluded for posting purposes.  I originally had my tascii function in a seperate cpp file to keep it clean*/
//#include "functions.cpp"//Custom file

using namespace std;	//So standard namespace classes can be used


/*
This function here should take a character and return its ASCII table equivalent.
*/
int tascii(char nchar[1])
{
	int result;

	if(strcmp(nchar,"A"))
	{
		result = 65;
	}

	if(strcmp(nchar,"B"))
	{
		result = 66;
	}

	if(strcmp(nchar,"C"))
	{
		result = 67;
	}

	if(strcmp(nchar,"D"))
	{
		result = 68;
	}

	if(strcmp(nchar,"E"))
	{
		result = 69;
	}

	if(strcmp(nchar,"F"))
	{
		result = 70;
	}

	if(strcmp(nchar,"G"))
	{
		result = 71;
	}

	if(strcmp(nchar,"H"))
	{
		result = 72;
	}

	if(strcmp(nchar,"I"))
	{
		result = 73;
	}

	if(strcmp(nchar,"J"))
	{
		result = 74;
	}

	if(strcmp(nchar,"K"))
	{
		result = 75;
	}

	if(strcmp(nchar,"L"))
	{
		result = 76;
	}

	if(strcmp(nchar,"M"))
	{
		result = 77;
	}

	if(strcmp(nchar,"N"))
	{
		result = 78;
	}

	if(strcmp(nchar,"O"))
	{
		result = 79;
	}

	if(strcmp(nchar,"P"))
	{
		result = 80;
	}

	if(strcmp(nchar,"Q"))
	{
		result = 81;
	}

	if(strcmp(nchar,"R"))
	{
		result = 82;
	}

	if(strcmp(nchar,"S"))
	{
		result = 83;
	}

	if(strcmp(nchar,"T"))
	{
		result = 84;
	}

	if(strcmp(nchar,"U"))
	{
		result = 85;
	}

	if(strcmp(nchar,"V"))
	{
		result = 86;
	}

	if(strcmp(nchar,"W"))
	{
		result = 87;
	}

	if(strcmp(nchar,"X"))
	{
		result = 88;
	}

	if(strcmp(nchar,"Y"))
	{
		result = 89;
	}

	if(strcmp(nchar,"Z"))
	{
		result = 90;
	}

	return result;
}


int _tmain()
{
	char letter[1];
	int result;


	cout << "Hello, I will be taking your text and turning it into its ASCII counterpart." << endl;
	cout << "Input a letter, single letter only please:\t";

	cin >> letter[1];

	cout << endl << "The letter you typed is:\t" << letter[1] << endl;

	result = tascii(letter);

	cout << "That letter converts to " << result << " in ASCII." << endl;


	return 0;
}
You would not include functions.cpp in main.cpp. Never include .cpp files in other .cpp files.

My guess is then you are compiling functions.cpp and main.cpp and trying to link them together. Since main.cpp included functions.cpp, main.o already had everything functions.cpp had.

BTW, your tascii function is wrong. You are getting lucky because of stack usage. You want to pass a char to the function, not a char[1], and then compare the character if( nchar == 'A' ).

Also the function is unnecessary

char ch = 'A';
cout << static_cast<int>( ch );

does the same thing as your code.
I have functions.cpp commented out in this code. In my code before I posted this I had the main cpp file and then I had the functions.cpp and the function above was not in the main.cpp it was in the functions.cpp. I hope I am being clear. Basically, I consolidated them for posting purpses. I now see what I did wrong though, so thank you. I originally tried ( nchar == "A"). When that had problems, I resolved to the method above. I never tried ( nchar == 'A').
That won't work unless you change nchar from char[1] to char.

Also, make sure you are not linking against functions.obj. Since your project was set up to have two source files, I'm sure the make system is still compiling and linking both.

I changed nchar back to a char instead of a char[1]. I used the if(nchar == 'X') instead and all is working as I intended it to. My original mistake was if(nchar == "X") and that mistake just evolved into more mistakes. There is a lot more for me to do to get this program where I want it but this hurdle has been passed. Thanks a lot!
Ok, when I removed that working function from main.cpp and put it back into functions.cpp I again had linking problems. I did use #include "functions.cpp". Is there a way I can show both files and maybe you can see what is going on??
Ok, I have confirmed this. When I put int tascii(char nchar) in it's own .cpp file it will not build. If i take it out of it's own .cpp file and delete that file from the project I do not have any problems. Am I having some kind of #include problem??
I have discovered that if I make my functions file a functions.h file instead of functions.cpp file my problems go away. What am I misunderstanding here?
Topic archived. No new replies allowed.