Why am I getting these “already defined” linker errors?

I am starting in C ++, but I have experience using Java. I get some errors that I don’t understand. I attached the pic. Console errors and code below.

 Error  1   error LNK2005: "public: __thiscall VectorDouble::VectorDouble(void)" (??0VectorDouble@@QAE@XZ) already defined in Main.obj  C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\VectorDouble.obj  Lab8_VectorDoubleClass

 Error  2   error LNK2005: "public: __thiscall VectorDouble::VectorDouble(int)" (??0VectorDouble@@QAE@H@Z) already defined in Main.obj  C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\VectorDouble.obj  Lab8_VectorDoubleClass
....    

10 more such errors and

 Error  13  error LNK1169: one or more multiply defined symbols found   C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\Debug\Lab8_VectorDoubleClass.exe  1   1   Lab8_VectorDoubleClass

main.cpp


#include "VectorDouble.cpp"
using namespace std;
void printVD(const VectorDouble& v);
int main()
{
    VectorDouble p;
    p.push_back(1);
    p.push_back(4);
    p.push_back(3);
    VectorDouble v(p);
    printVD(v);
    printVD(p);
}
void printVD(const VectorDouble& v)
{
    int n = v.size();
    for(int i = 0; i<n; i++)
    {
        cout << v.getElementAt(n) << " ";
    }
    cout << endl;
}

VectorDouble.h


#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;
class VectorDouble
{
public:
    VectorDouble(void);
    ~VectorDouble(void);
    VectorDouble(int intSize);
    // Copy constructor
    VectorDouble(const VectorDouble& vd);
    // = override
    void operator =(const VectorDouble& RIGHT_SIDE);
private:
    double *dArray;
    int count, max_count;
public:
    // returns number of occupied cells
    int size(void) const;
    // Returns total number of cells
    int capacity(void) const;
    // Adds an element to array
    void push_back(double num);
    // Resizes the array to be double the original max_count
    void resize(void);
    // Returns element at specified index
    double getElementAt(int i) const;
    // Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements
    void reserve(int n);
private:
    // Sets every element to 0
    void clear(void);
};

VectorDouble.cpp


    #pragma once
#include "VectorDouble.h"

using namespace std;

VectorDouble::VectorDouble(void)
{
    max_count = 100;
    count = 0;
    dArray = new double[max_count];
    clear();
}

VectorDouble::VectorDouble(int intSize)
{
    max_count = intSize;
    dArray = new double[max_count];
    clear();
}

VectorDouble::~VectorDouble(void)
{
    cout << "vector with " << this->count << " is destroyed";
}

// Copy constructor
VectorDouble::VectorDouble(const VectorDouble& vd) 
{
    int mcVD = vd.capacity(), i=0;
    max_count = mcVD;
    dArray = new double[max_count];
    clear();
    while(i<max_count)
    {
        dArray[i] = vd.getElementAt(i);
        i++;
    }
}
// = override
void VectorDouble::operator =(const VectorDouble& RIGHT_SIDE)
{
    int rightCount = RIGHT_SIDE.size(), i=0;
    while(rightCount>max_count)
    {
        resize();
    }
    while(i<rightCount)
    {
        dArray[i] = RIGHT_SIDE.getElementAt(i);
        i++;
    }
    count = i;
}
// returns number of occupied cells
int VectorDouble::size(void) const
{
    return count;
}
// Returns total number of cells
int VectorDouble::capacity(void) const
{
    return max_count;
}
// Adds an element to array
void VectorDouble::push_back(double num)
{
    if(count==max_count)
    {
        resize();
    }
    dArray[count] = num;
    count++;
}
// Resizes the array to be double the original max_count
void VectorDouble::resize(void)
{
    double *p = new double[max_count*2];
    for(int i = 0; i < count; i++)
    {
        p[i] = dArray[i];
    }
    dArray = p;
    max_count*=2;
    delete p;
}


// Returns element at specified index
double VectorDouble::getElementAt(int i) const
{
    return dArray[i];
}


// Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements
void VectorDouble::reserve(int n)
{
    while(n<max_count)
        resize();
}


// Sets every element to 0
void VectorDouble::clear(void)
{
    for(int i = 0; i < max_count; i++)
        dArray[i] = 0;
}

Any help would be greatly appreciated ...

+5
source share
4 answers

You should turn on "VectorDouble.h", not "VectorDouble.cpp"on Main.cpp.

The whole concept of include files in C ++ is different in different ways compared to many other languages.

++ "" "". - , , . VectorDouble.cpp , VectorDouble.h .

#include ++ . , . #include , .

#include , , , #include. "VectorDouble.cpp". , , , .

, . , ​​ inline, definition. , , inline . , , , .

, , . , , . , - . , , , .

+26

, , - , , -. :

public: __thiscall VectorDouble::VectorDouble(void)" (??0VectorDouble@@QAE@XZ) already defined in Main.obj

, , VectorDouble::VectorDouble().

, , , main.cpp:

#include "VectorDouble.cpp"

, , . , main.cpp , VectorDouble.cpp. , VectorDouble.cpp, : VectorDouble.cpp main.cpp.

, ,

#include "VectorDouble.h"

. , , #include.cpp . , .

, !

+6

Main.cpp #include -ing VectorDouble.cpp; #include VectorDouble.h. .cpp, VectorDouble.cpp ( , #include -d Main.cpp).

+1

, PresentDataStruct.h, PresentDataStruct.cpp.

PresentDataStruct.h:

#pragma once

#include <string>
using namespace System;
ref class CPresentDataStruct
{
public:
    CPresentDataStruct();
    ~CPresentDataStruct();

public:
    void SomeActionOnData();

public:

    System::String^ Name;
    DateTime^ BirthDay;

};

void CPresentDataStruct::SomeActionOnData()
{
    //TO DO here
}

CPresentDataStruct::CPresentDataStruct()
{
}

CPresentDataStruct::~CPresentDataStruct()
{
}

PresentDataStruct.cpp :

include "PresentDataStruct.h"

, include "PresentDataStruct.h" maindialog.h.

, PresentDataStruct.cpp, :

void CPresentDataStruct::SomeActionOnData()
{
    //TO DO here
}

CPresentDataStruct::CPresentDataStruct()
{
}

CPresentDataStruct::~CPresentDataStruct()
{
}
0

All Articles