0

I'm trying to link declaration part to realization part, but I don't know how to do it correctly. I've found one solution in Internet, but it doesn't work. I'll really appreciate your help if you know good sources on this topic.

Errors are:

error C2995: Couple<K, V>::Couple(K key, V value) template of function already defined

error C2995: void Couple<K, V>::setKey(K key) template of function already defined

..etc..

My code here

//Couple.h
#pragma once
#ifndef _COUPLE_H_
#define _COUPLE_H_
template<typename K, typename V>
class Couple {
private:
    K key;
    V value;
public:
    Couple(K key, V value);
    void setKey(K key);
    void setValue(V value);
    K getKey();
    V getValue();
};
#include "Couple.cpp"
#endif


//Couple.cpp
#include "Couple.h"

template<typename K, typename V>
Couple<K, V>::Couple(K key, V value){
    Couple<K, V>::key = key;
    Couple<K, V>::value = value;
}

template<typename K, typename V>
void Couple<K, V>::setKey(K key) {
    Couple<K, V>::key = key;
}

template<typename K, typename V>
void Couple<K, V>::setValue(V value) {
    Couple<K, V>::value = value;
}

template<typename K, typename V>
K Couple<K, V>::getKey() {
    return Couple<K, V>::key;
}

template<typename K, typename V>
V Couple<K, V>::getValue() {
    return Couple<K, V>::value;
}


//main.cpp
#include<iostream>
#include<string>
#include "Couple.h"
using namespace std;

int main() 
{
    Couple<string, int> person = Couple<string, int>("John", 20);
    cout << person.getKey() << " " << person.getValue();
}

enter image description here

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Angedon
  • 1
  • 1
  • 1
    Long story short - templates are supposed to have both declaration and definition in the same file. – The Dreams Wind Jul 22 '23 at 14:48
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_COUPLE_H_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Jul 22 '23 at 14:48
  • 3
    `Couple.h` includes `Couple.cpp` which includes `Couple.h`... A classic circular include dependency. – Some programmer dude Jul 22 '23 at 14:49
  • 1
    Related: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) and [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Jul 22 '23 at 14:49
  • 1
    The solution for both of these problems is to get rid of the `.cpp` file and move the template implementation into the header. – drescherjm Jul 22 '23 at 14:52
  • @Someprogrammerdude -- the circular include is harmless here. – Pete Becker Jul 22 '23 at 14:52
  • Please [edit] your question and copy-paste (as text!) the full and complete build-log from the example you show. – Some programmer dude Jul 22 '23 at 14:53
  • The code as shown ought to work. There's something else going on that's not coming from this code. Add the **full** error messages that you're getting; they'll tell you where the duplicate definitions are coming from, and that will usually help in seeing what's causing them. The advice you're getting in the comments is generic stuff, and good ideas on their own, but the way this code is structured the advice won't help. – Pete Becker Jul 22 '23 at 14:55
  • @Some programmer dude logs in my native language :(( – Angedon Jul 22 '23 at 14:57
  • 1
    You can change the regional settings temporarily to get error messages in English. Seeing the actual error messages can often help, especially if there are informational notes. – Some programmer dude Jul 22 '23 at 14:59
  • Something is happening wrong and I cannot install english :((, but I translated errors above into english litteraly! – Angedon Jul 22 '23 at 15:12
  • @drescherjm -- yes, but with the #include "Couple.cpp"` in the header, and using #include "Couple.h"` in the main file, this would work. There is something else going on that's not shown here. – Pete Becker Jul 22 '23 at 15:14
  • 1
    The first step is to delete couple.cpp and move all template code into couple.h. Once that is done then it will be easier to sort out any remaining problems. – john Jul 22 '23 at 15:15
  • Is this an Intellisense Error or a real compiler error? msvc compiler errors start with C and have a 4 digit numerical code. – drescherjm Jul 22 '23 at 15:15
  • @drescherjm, it's compile error. – Angedon Jul 22 '23 at 15:17
  • @john, yes, I can do that, but I thought that I can somehow divide these parts, if I know right, we should seperate our declaration and initialization parts. – Angedon Jul 22 '23 at 15:19
  • Templates are different from non-templated code. Also its not a good practice to give a template implementation file a `.cpp` extension. I can see from your picture that this file is part of the project and being compiled separately into an .obj and also included from `Couple.h`. However I don't believe this should cause a problem. This is why I asked if the error was a compiler versus intellisense error. Intellisense is a separate compiler that is built for speed and it will have false positives. – drescherjm Jul 22 '23 at 15:19
  • @Angedon Normally yes, but not for templates. For templates all the code must go in the header file. There is no .cpp file for a template. – john Jul 22 '23 at 15:21
  • @drescherjm, sorry for wrong image, there is C**** and so it's about compiler. I fixed image. – Angedon Jul 22 '23 at 15:23
  • Related question (now that I see the error code): [https://stackoverflow.com/questions/11094310/error-c2995-function-template-has-already-been-defined](https://stackoverflow.com/questions/11094310/error-c2995-function-template-has-already-been-defined) – drescherjm Jul 22 '23 at 15:24
  • @john, you mean that if I write my code with templates all in header, it will be ok? Not an example of a bad code? – Angedon Jul 22 '23 at 15:25
  • @Angedon It's not completely clear to me what is wrong with your code, but you goal,(trying to separate declaration from definition in a template) cannot be achieved. – john Jul 22 '23 at 15:25
  • @Angedon That is how everyone writes templates. – john Jul 22 '23 at 15:25
  • My advice is to remove `Couple.cpp` from your project in Solution Explorer then rename it to `Couple.inl` using your file explorer and fix the header to include the new filename. Related: [https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention](https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention) and finally don't have `Couple.inl` include `Couple.h` – drescherjm Jul 22 '23 at 15:27
  • drescherjm, john, both of your solutions works for me! Thank you all for answering me! – Angedon Jul 22 '23 at 15:37

0 Answers0