undefined reference to template class member
I am getting an undefined reference to my Pairwise class after I attempt to run it with sample input. My Pairwise class was made in a headerfile, and I made a main function to pass in key and value as strings to format them as I'd like.
My expected output:
key:value
My headerfile:
#pragma once
#include<iostream>
using std::ostream; using std::cout; using std::endl;
#include<string>
using std::string;
#include<sstream>
using std::ostringstream;
template<typename K, typename V>
struct Pairwise {
K first;
V second;
Pairwise() = default;
Pairwise(K,V);
friend ostream& operator<<(ostream &out, const Pairwise &p){
out << p.first << ":" << p.second;
return out;
}
};
My main function:
#include "file.h"
#include<iostream>
using std::cout; using std::endl;
#include<sstream>
using std::ostringstream;
#include<string>
using std::string;
int main()
{
Pairwise<string,string> p("key", "value");
ostringstream oss;
oss<<p;
string example = oss.str();
cout << example << endl;
}
The error that I am getting:
/tmp/ccfv4iaY.o: In function `main':
main_program.cpp:(.text+0x8b): undefined reference to `Pairwise<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Pairwise(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
c++
add a comment |
I am getting an undefined reference to my Pairwise class after I attempt to run it with sample input. My Pairwise class was made in a headerfile, and I made a main function to pass in key and value as strings to format them as I'd like.
My expected output:
key:value
My headerfile:
#pragma once
#include<iostream>
using std::ostream; using std::cout; using std::endl;
#include<string>
using std::string;
#include<sstream>
using std::ostringstream;
template<typename K, typename V>
struct Pairwise {
K first;
V second;
Pairwise() = default;
Pairwise(K,V);
friend ostream& operator<<(ostream &out, const Pairwise &p){
out << p.first << ":" << p.second;
return out;
}
};
My main function:
#include "file.h"
#include<iostream>
using std::cout; using std::endl;
#include<sstream>
using std::ostringstream;
#include<string>
using std::string;
int main()
{
Pairwise<string,string> p("key", "value");
ostringstream oss;
oss<<p;
string example = oss.str();
cout << example << endl;
}
The error that I am getting:
/tmp/ccfv4iaY.o: In function `main':
main_program.cpp:(.text+0x8b): undefined reference to `Pairwise<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Pairwise(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
c++
3
You've declared aPairwiseconstructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.
– Igor Tandetnik
Nov 25 '18 at 3:30
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38
add a comment |
I am getting an undefined reference to my Pairwise class after I attempt to run it with sample input. My Pairwise class was made in a headerfile, and I made a main function to pass in key and value as strings to format them as I'd like.
My expected output:
key:value
My headerfile:
#pragma once
#include<iostream>
using std::ostream; using std::cout; using std::endl;
#include<string>
using std::string;
#include<sstream>
using std::ostringstream;
template<typename K, typename V>
struct Pairwise {
K first;
V second;
Pairwise() = default;
Pairwise(K,V);
friend ostream& operator<<(ostream &out, const Pairwise &p){
out << p.first << ":" << p.second;
return out;
}
};
My main function:
#include "file.h"
#include<iostream>
using std::cout; using std::endl;
#include<sstream>
using std::ostringstream;
#include<string>
using std::string;
int main()
{
Pairwise<string,string> p("key", "value");
ostringstream oss;
oss<<p;
string example = oss.str();
cout << example << endl;
}
The error that I am getting:
/tmp/ccfv4iaY.o: In function `main':
main_program.cpp:(.text+0x8b): undefined reference to `Pairwise<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Pairwise(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
c++
I am getting an undefined reference to my Pairwise class after I attempt to run it with sample input. My Pairwise class was made in a headerfile, and I made a main function to pass in key and value as strings to format them as I'd like.
My expected output:
key:value
My headerfile:
#pragma once
#include<iostream>
using std::ostream; using std::cout; using std::endl;
#include<string>
using std::string;
#include<sstream>
using std::ostringstream;
template<typename K, typename V>
struct Pairwise {
K first;
V second;
Pairwise() = default;
Pairwise(K,V);
friend ostream& operator<<(ostream &out, const Pairwise &p){
out << p.first << ":" << p.second;
return out;
}
};
My main function:
#include "file.h"
#include<iostream>
using std::cout; using std::endl;
#include<sstream>
using std::ostringstream;
#include<string>
using std::string;
int main()
{
Pairwise<string,string> p("key", "value");
ostringstream oss;
oss<<p;
string example = oss.str();
cout << example << endl;
}
The error that I am getting:
/tmp/ccfv4iaY.o: In function `main':
main_program.cpp:(.text+0x8b): undefined reference to `Pairwise<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Pairwise(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
c++
c++
asked Nov 25 '18 at 3:28
DracepDracep
10010
10010
3
You've declared aPairwiseconstructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.
– Igor Tandetnik
Nov 25 '18 at 3:30
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38
add a comment |
3
You've declared aPairwiseconstructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.
– Igor Tandetnik
Nov 25 '18 at 3:30
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38
3
3
You've declared a
Pairwise constructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.– Igor Tandetnik
Nov 25 '18 at 3:30
You've declared a
Pairwise constructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.– Igor Tandetnik
Nov 25 '18 at 3:30
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38
add a comment |
1 Answer
1
active
oldest
votes
Your template contains a declaration for Pairwise(K,V);, but it is never defined / implemented anywhere.
You need to add the definition in the same (or another) header file. To verify, replace the ; by {}, and the linker error should be gone: Pairwise(K,V) {} (it won't work but it will compile and link fine).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464403%2fundefined-reference-to-template-class-member%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your template contains a declaration for Pairwise(K,V);, but it is never defined / implemented anywhere.
You need to add the definition in the same (or another) header file. To verify, replace the ; by {}, and the linker error should be gone: Pairwise(K,V) {} (it won't work but it will compile and link fine).
add a comment |
Your template contains a declaration for Pairwise(K,V);, but it is never defined / implemented anywhere.
You need to add the definition in the same (or another) header file. To verify, replace the ; by {}, and the linker error should be gone: Pairwise(K,V) {} (it won't work but it will compile and link fine).
add a comment |
Your template contains a declaration for Pairwise(K,V);, but it is never defined / implemented anywhere.
You need to add the definition in the same (or another) header file. To verify, replace the ; by {}, and the linker error should be gone: Pairwise(K,V) {} (it won't work but it will compile and link fine).
Your template contains a declaration for Pairwise(K,V);, but it is never defined / implemented anywhere.
You need to add the definition in the same (or another) header file. To verify, replace the ; by {}, and the linker error should be gone: Pairwise(K,V) {} (it won't work but it will compile and link fine).
answered Nov 25 '18 at 3:33
AganjuAganju
5,1641721
5,1641721
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464403%2fundefined-reference-to-template-class-member%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
You've declared a
Pairwiseconstructor taking two parameters, but you haven't actually implemented it. You are nevertheless trying to use it.– Igor Tandetnik
Nov 25 '18 at 3:30
@IgorTandetnik Ah I see what you mean! How could I implement Pairwise constructor to print out key:value if you don't mind?
– Dracep
Nov 25 '18 at 3:38