I'm writing a program and am getting both an LNK2005 and LNK1169 error. I might be doing something wrong with trying to call the various files as this is the first program Ive tried writing with more than one. I was given a .h file to begin with that I cant change:

#ifndef CIRCLE  
#define CIRCLE  

using namespace std;

class Circle
{
        Circle();
        ~Circle(){} 

        vector<string> getNames();
        vector<string> playGame(int n, int m);
        int reportSafeIndex(int n, int m);
};  
#endif  

Then I wrote the following:

#include "CIRCLE.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class Circle    
{


public:

    string getNames();
    string playGame(int m, int n);
    int reportSafeIndex (int n, int m);

private:

    string name[20];
    int n;
    int m;

};


string Circle :: getNames ()
{
    string list;
    //initialize the 20 names

    name [0]="Alice";
    name [1]="Brent";
    name [2]="Carly";
    name [3]="Dan";
    name [4]="Emily";
    name [5]="Fred";
    name [6]="George";
    name [7]="Henry";
    name [8]="Isabel";
    name [9]="Josephus";
    name [10]="Katie";
    name [11]="Lauren";
    name [12]="Matt";
    name [13]="Nicole";
    name [14]="Olivia";
    name [15]="Pam";
    name [16]="Quinten";
    name [17]="Ron";
    name [18]="Silvia";
    name [19]="Tim";

//Output the names given
    int i;
    for (i=0; i<20; i++)
    {
        list = list + " " + name[i];
    }
    cout << list;
    return list;
}
// Begins playing the game, running through the people
string Circle :: playGame(int m, int n)
{
    int i;
    string name;
    string die;

    while (n < 10 || n > 20)
    {
        cout << "How many people (10-20) would you like to play with?" << endl;
        cin >> n;
    }

    while (m < 1)
    {
        cout << "How many people would you like to skip each time?" << endl;
        cin >> m;
    }

    for (i=0; i<n; i++)
    {
        die = name[(i*m)%n];
    }
    cout << die;
    return die;
}

// Reports where the user should stand to be the last person remaining, returns -1 if outside perameters.
int Circle :: reportSafeIndex (int n, int m)
{
    int i;
    int safe;

    cout << "How many people (10-20) would you like to play with?" << endl;
    cin >> n;

    cout << "How many people would you like to skip each time?" << endl;
    cin >> m;

    if (n < 10 || n > 20 || m < 1)
    {
        return -1;
    }
    else
    {
        for (i=0; i<n; i++)
        {
            safe=((i*m)%n+1);
        }
        cout << safe;
        return safe;
    }
}

And lastly I wrote a main:

#include "CIRCLE.h"
#include <iostream>
#include <string>
#include "Josephus.cpp"
using namespace std;
int main()
{   int m; int n;
    Circle game1;
    game1.getNames();
    game1.playGame(m, n);
    game1.reportSafeIndex(n, m);
}

I beleive the problem is in the second file.

If anyone can help me it would be greatly appreciated. Thanks

Recommended Answers

All 3 Replies

In your .cpp file you are redefining the Circle class, which was already defined in the header. This would certainly cause a linker error.

Remove the entire class statement from your cpp file (lines 7-24 in your 2nd snippet) and you should be good to go.

So when I do that the header file has the functions described as vector<string> ... and theyre not compatible with the rest of my program. How should I change the functions to allow them to agree?

Sorry I didn't notice that. Yes, you need to make sure that the functions in your header always have the same signature as those in your .cpp file.
Currently there is a mismatch, your header says that the function returns a std::vector of std::strings, but the version in your .cpp file returns a std::string.
So you either need to change the function prototype in the header to return a std::string, or you alter the implementation in your .cpp file to return a vector of strings (which will mean updating the functions signature and updating the code to use a vector of strings)

In fact, looking at your code from an OO point of view, I'd recommend using a vector of strings for the 'name' member variable (rather than an array of strings) and make getNames return a vector of strings as stated in your header. You should also consider creating a constructor for your class which will populate the 'name' member variable of your class at instantiation, rather than doing it each time getNames is called. Then your getNames function can simply return a copy of the 'name' member variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.