undefined reference to function declared in header and implemented in cpp file
I'm working on some basic C++ code that uses two cpp files(Main.cpp and CarbStore.cpp) and one header(CarbStore.h). Within my header I have declared a function that is later implemented in CarbStore.cpp. When I call the function from my Main.cpp it gives me the error:
Main.cpp:17: undefined reference to `CarbStore::CalcCarbs(unsigned char, unsigned char, unsigned char, float, unsigned int, std::__cxx11::basic_string, std::allocator >) const'
My files contain the following code:
Main.cpp
#include <iostream>
#include <cstdint>
#include <cmath>
#include <ctime>
#include "CarbStore.h"
void CarbCalculator()
{
CarbStore carb;
carb.CalcCarbs(10, 11, 12, 0.1, 100, "test");
}
int main(int,char *)
{
CarbCalculator();
std::cout << "Press enter to exit." << std::endl;
std::cin.get();
}
CarbStore.cpp
#include "CarbStore.h"
#include <iostream>
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
CarbStore.h
#ifndef CARBSTORE_H
#define CARBSTORE_H
#include <vector>
class CarbStore
{
public:
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const;
};
#endif
c++ function linker-errors header-files
add a comment |
I'm working on some basic C++ code that uses two cpp files(Main.cpp and CarbStore.cpp) and one header(CarbStore.h). Within my header I have declared a function that is later implemented in CarbStore.cpp. When I call the function from my Main.cpp it gives me the error:
Main.cpp:17: undefined reference to `CarbStore::CalcCarbs(unsigned char, unsigned char, unsigned char, float, unsigned int, std::__cxx11::basic_string, std::allocator >) const'
My files contain the following code:
Main.cpp
#include <iostream>
#include <cstdint>
#include <cmath>
#include <ctime>
#include "CarbStore.h"
void CarbCalculator()
{
CarbStore carb;
carb.CalcCarbs(10, 11, 12, 0.1, 100, "test");
}
int main(int,char *)
{
CarbCalculator();
std::cout << "Press enter to exit." << std::endl;
std::cin.get();
}
CarbStore.cpp
#include "CarbStore.h"
#include <iostream>
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
CarbStore.h
#ifndef CARBSTORE_H
#define CARBSTORE_H
#include <vector>
class CarbStore
{
public:
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const;
};
#endif
c++ function linker-errors header-files
2
Your definition ofCalcCarbs
is a non-member function. Usevoid CarbStore::CalcCarbs() { ... }
to make it a member function.
– R Sahu
Nov 26 '18 at 22:50
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53
add a comment |
I'm working on some basic C++ code that uses two cpp files(Main.cpp and CarbStore.cpp) and one header(CarbStore.h). Within my header I have declared a function that is later implemented in CarbStore.cpp. When I call the function from my Main.cpp it gives me the error:
Main.cpp:17: undefined reference to `CarbStore::CalcCarbs(unsigned char, unsigned char, unsigned char, float, unsigned int, std::__cxx11::basic_string, std::allocator >) const'
My files contain the following code:
Main.cpp
#include <iostream>
#include <cstdint>
#include <cmath>
#include <ctime>
#include "CarbStore.h"
void CarbCalculator()
{
CarbStore carb;
carb.CalcCarbs(10, 11, 12, 0.1, 100, "test");
}
int main(int,char *)
{
CarbCalculator();
std::cout << "Press enter to exit." << std::endl;
std::cin.get();
}
CarbStore.cpp
#include "CarbStore.h"
#include <iostream>
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
CarbStore.h
#ifndef CARBSTORE_H
#define CARBSTORE_H
#include <vector>
class CarbStore
{
public:
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const;
};
#endif
c++ function linker-errors header-files
I'm working on some basic C++ code that uses two cpp files(Main.cpp and CarbStore.cpp) and one header(CarbStore.h). Within my header I have declared a function that is later implemented in CarbStore.cpp. When I call the function from my Main.cpp it gives me the error:
Main.cpp:17: undefined reference to `CarbStore::CalcCarbs(unsigned char, unsigned char, unsigned char, float, unsigned int, std::__cxx11::basic_string, std::allocator >) const'
My files contain the following code:
Main.cpp
#include <iostream>
#include <cstdint>
#include <cmath>
#include <ctime>
#include "CarbStore.h"
void CarbCalculator()
{
CarbStore carb;
carb.CalcCarbs(10, 11, 12, 0.1, 100, "test");
}
int main(int,char *)
{
CarbCalculator();
std::cout << "Press enter to exit." << std::endl;
std::cin.get();
}
CarbStore.cpp
#include "CarbStore.h"
#include <iostream>
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
CarbStore.h
#ifndef CARBSTORE_H
#define CARBSTORE_H
#include <vector>
class CarbStore
{
public:
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const;
};
#endif
c++ function linker-errors header-files
c++ function linker-errors header-files
asked Nov 26 '18 at 22:46
OberrukOberruk
83
83
2
Your definition ofCalcCarbs
is a non-member function. Usevoid CarbStore::CalcCarbs() { ... }
to make it a member function.
– R Sahu
Nov 26 '18 at 22:50
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53
add a comment |
2
Your definition ofCalcCarbs
is a non-member function. Usevoid CarbStore::CalcCarbs() { ... }
to make it a member function.
– R Sahu
Nov 26 '18 at 22:50
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53
2
2
Your definition of
CalcCarbs
is a non-member function. Use void CarbStore::CalcCarbs() { ... }
to make it a member function.– R Sahu
Nov 26 '18 at 22:50
Your definition of
CalcCarbs
is a non-member function. Use void CarbStore::CalcCarbs() { ... }
to make it a member function.– R Sahu
Nov 26 '18 at 22:50
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53
add a comment |
1 Answer
1
active
oldest
votes
As already told in the comments, the following
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
does not implement the member function CalcCarbs
of CarbStore
, but instead it declares and defines a new free function called CalcCarbs
. To implement the member function you need to tell the compiler to which class the function definition should belong. This is done by appending the class name together with a double colon before the function name:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
The signature must also match. In CarbStore
you declared the function const
, but you did not do so in the implementation. To correct it:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const
{
//use values at later state
return;
}
However it is highly unlikely that you really want a const
member function with empty return, because such a function can only have a lasting effect by modifying global variables or doing IO.
Furthermore, but unrelated to this particular error message:
- In
CarbStore
you are using astd::string
, so you need to#include<string>
. On the other hand I don't see anystd::vector
in it, so the#include<vector>
seems unnecessary (as do all the other includes besidesiostream
inMain.cpp
). - The
return;
at the end of the function body is also point-less for avoid
function. - If
main
doesn't use the command line arguments, you can also give it the signatureint main()
.
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%2f53490271%2fundefined-reference-to-function-declared-in-header-and-implemented-in-cpp-file%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
As already told in the comments, the following
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
does not implement the member function CalcCarbs
of CarbStore
, but instead it declares and defines a new free function called CalcCarbs
. To implement the member function you need to tell the compiler to which class the function definition should belong. This is done by appending the class name together with a double colon before the function name:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
The signature must also match. In CarbStore
you declared the function const
, but you did not do so in the implementation. To correct it:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const
{
//use values at later state
return;
}
However it is highly unlikely that you really want a const
member function with empty return, because such a function can only have a lasting effect by modifying global variables or doing IO.
Furthermore, but unrelated to this particular error message:
- In
CarbStore
you are using astd::string
, so you need to#include<string>
. On the other hand I don't see anystd::vector
in it, so the#include<vector>
seems unnecessary (as do all the other includes besidesiostream
inMain.cpp
). - The
return;
at the end of the function body is also point-less for avoid
function. - If
main
doesn't use the command line arguments, you can also give it the signatureint main()
.
add a comment |
As already told in the comments, the following
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
does not implement the member function CalcCarbs
of CarbStore
, but instead it declares and defines a new free function called CalcCarbs
. To implement the member function you need to tell the compiler to which class the function definition should belong. This is done by appending the class name together with a double colon before the function name:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
The signature must also match. In CarbStore
you declared the function const
, but you did not do so in the implementation. To correct it:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const
{
//use values at later state
return;
}
However it is highly unlikely that you really want a const
member function with empty return, because such a function can only have a lasting effect by modifying global variables or doing IO.
Furthermore, but unrelated to this particular error message:
- In
CarbStore
you are using astd::string
, so you need to#include<string>
. On the other hand I don't see anystd::vector
in it, so the#include<vector>
seems unnecessary (as do all the other includes besidesiostream
inMain.cpp
). - The
return;
at the end of the function body is also point-less for avoid
function. - If
main
doesn't use the command line arguments, you can also give it the signatureint main()
.
add a comment |
As already told in the comments, the following
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
does not implement the member function CalcCarbs
of CarbStore
, but instead it declares and defines a new free function called CalcCarbs
. To implement the member function you need to tell the compiler to which class the function definition should belong. This is done by appending the class name together with a double colon before the function name:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
The signature must also match. In CarbStore
you declared the function const
, but you did not do so in the implementation. To correct it:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const
{
//use values at later state
return;
}
However it is highly unlikely that you really want a const
member function with empty return, because such a function can only have a lasting effect by modifying global variables or doing IO.
Furthermore, but unrelated to this particular error message:
- In
CarbStore
you are using astd::string
, so you need to#include<string>
. On the other hand I don't see anystd::vector
in it, so the#include<vector>
seems unnecessary (as do all the other includes besidesiostream
inMain.cpp
). - The
return;
at the end of the function body is also point-less for avoid
function. - If
main
doesn't use the command line arguments, you can also give it the signatureint main()
.
As already told in the comments, the following
void CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
does not implement the member function CalcCarbs
of CarbStore
, but instead it declares and defines a new free function called CalcCarbs
. To implement the member function you need to tell the compiler to which class the function definition should belong. This is done by appending the class name together with a double colon before the function name:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer)
{
//use values at later state
return;
}
The signature must also match. In CarbStore
you declared the function const
, but you did not do so in the implementation. To correct it:
void CarbStore::CalcCarbs(unsigned char r, unsigned char b, unsigned char g, float bounciness, unsigned int price, std::string manufacturer) const
{
//use values at later state
return;
}
However it is highly unlikely that you really want a const
member function with empty return, because such a function can only have a lasting effect by modifying global variables or doing IO.
Furthermore, but unrelated to this particular error message:
- In
CarbStore
you are using astd::string
, so you need to#include<string>
. On the other hand I don't see anystd::vector
in it, so the#include<vector>
seems unnecessary (as do all the other includes besidesiostream
inMain.cpp
). - The
return;
at the end of the function body is also point-less for avoid
function. - If
main
doesn't use the command line arguments, you can also give it the signatureint main()
.
answered Nov 26 '18 at 23:58
user10605163user10605163
2,858624
2,858624
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%2f53490271%2fundefined-reference-to-function-declared-in-header-and-implemented-in-cpp-file%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
2
Your definition of
CalcCarbs
is a non-member function. Usevoid CarbStore::CalcCarbs() { ... }
to make it a member function.– R Sahu
Nov 26 '18 at 22:50
You wrote a ::CalcCarbs() function, it is not an implementation of the CalcCarbs class constructor. Write CalcCarbs::CalCarbs(...) to get ahead.
– Hans Passant
Nov 26 '18 at 22:51
They are both being compiled together in my makefile, hence my confusion.
– Oberruk
Nov 26 '18 at 22:53