Duplicate symbols

Hey y'all,

I have the followingcode:
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
/*
 *  Startup.cpp
 *  MyMultiplication
 *
 *  Created by Nicky on 6/1/13.
 *  Copyright 2013 __MyCompanyName__. All rights reserved.
 *
 */

#include "Startup.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
StartProfile::question::question(){
	a = rand() % 12 + 1;
	b = rand() % 12 + 1;
	answer = a*b;
};
void StartProfile::setup(){
	vector<StartProfile::Player> players;
	vector<StartProfile::Player> pref;
	vector<string> names;
	string num;
	int qcount;
	pref.reserve(4);
	players.reserve(4);
	string xplayercount;
	cout << "Hello, and welcome to MyMultiplication BETA 1.0!\n";
	sleep(1);
	cout << "This is a 1 to 4 player multiplication game for all ages!\n";
	sleep(1);
	cout << "Enter number of players: ";
	getline(cin, xplayercount);
	int playercount;
	stringstream(xplayercount) >> playercount; 	
	for (int i = 1;i <= playercount; i++) {
		cout << "Player " << i <<", please enter your name: ";
		getline(cin, pref[i-1].name);
		players.push_back (pref[i-1]);
		cout << "\nAlright, " << players[i-1].name << "! You are ready to go.\n";
	};
	cout << "Enter Number Of Questions! ";
	getline(cin, num);
	stringstream(num) >> qcount;
	cout << "Everyone is checked in! Lets start!";
};
void StartProfile::askandrecord(StartProfile::Player& player, int count){
	cout << player.name << ", you are starting!\n";
	sleep(1);
	cout << '3\n';
	sleep(1);
	cout <<  '2\n';
	sleep(1);
	cout << '1\n';
	sleep(1);
	cout << "GO\n";
	
	
};

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
/*
 *  Startup.h
 *  MyMultiplication
 *
 *  Created by Nicky on 6/1/13.
 *  Copyright 2013 SaltPepper. All rights reserved.
 *
 */
#ifndef STARTUP_H
#define STARTUP_H
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
enum outcomes{WRONG, RIGHT, OUT_OF_TIME};
class StartProfile{
public:
	typedef class question{
	public:
		question::question();
		int a;
		int b;
		int answer;
		int whatusaid;
		outcomes outcome;
	};	
	class Player{
	public:
		int p;
		string name;
		int score;
		vector<StartProfile::question> questions;
	};	
	void askandrecord(StartProfile::Player& player, int count);
	void getresults(StartProfile::Player& player);
	void setup();
	StartProfile();
	~StartProfile();
};
#endif

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Startup.h"
#include "Startup.cpp"
using namespace std;
int main (int argc, char * const argv[]) {
	srand(time(NULL));
	StartProfile MASTERPROFILE;
	MASTERPROFILE.setup();
	return 0;
}

When I run, I get this error:

Line Location Tool:0: Command /Developer/usr/bin/g++-4.0 failed with exit code 1
Line Location Tool:0: duplicate symbol StartProfile::question::question()in /Users/Nicky/Desktop/MyMultiplication/build/MyMultiplication.build/Debug/MyMultiplication.build/Objects-normal/i386/Startup.o and /Users/Nicky/Desktop/MyMultiplication/build/MyMultiplication.build/Debug/MyMultiplication.build/Objects-normal/i386/main.o

But the question is, where is the duplicate symbol?
for starters on line 16 remove one of the "::question".

in fact why are you doing this in your header file too??
Last edited on
Why are you including a source file in your main source file? That's extremely non-standard.

My guess is that your IDE/compiler is compiling Startup.cpp into its own object file containing a definition of that method, and then compiling main.cpp - which includes Startup.cpp - into an object file which also contains a definition for that method.

Then, when it links them, it discovers that there are two definitions, one in in each object file.

Solution: don't include Startup.cpp in main.cpp

Edit: also, what mutexe said.
Last edited on
Well, following both of yours ideas, I get these errors:
Line Location Startup.cpp:16: error: no 'int StartProfile::question()' member function declared in class 'StartProfile'
Line Location Startup.cpp:16: error: ISO C++ forbids declaration of 'question' with no type
Line Location Startup.cpp:17: error: 'a' was not declared in this scope
Line Location Startup.cpp:18: error: 'b' was not declared in this scope
Line Location Startup.cpp:19: error: 'answer' was not declared in this scope

Note that StartProfile::question::question is a constructor for the question type, which is a class containing a, b, and answer.
Ah - hadn't spotted that question was itself a class, nested inside the Startup class. In that case, it should be restored to how you previously had it. My apologies.

Don't put back the include statement, though!
Wait, could I un-typedef it? That might work, as I don't know if question is a member of StartProfile, just a decleration.
Hokay, after un-typedefing question, i get two errors.

Line Location Tool:0: "StartProfile::~StartProfile()", referenced from:
Line Location Tool:0: _main in main.o
Line Location Tool:0: "StartProfile::StartProfile()", referenced from:

Why is this occuring, what is main.o?
main.o is the object file created from your main.cpp file.

I don't think you've shown us the complete error message, so we can't tell you what's wrong.
i get two errors.
You didn't implement those functions (constructor/destructor)
Topic archived. No new replies allowed.