Reputation: 13
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
Upvotes: 0
Views: 3444
Reputation:
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:
CarbStore
you are using a std::string
, so you need to #include<string>
. On the other hand I don't see any std::vector
in it, so the #include<vector>
seems unnecessary (as do all the other includes besides iostream
in Main.cpp
).return;
at the end of the function body is also point-less for a void
function.main
doesn't use the command line arguments, you can also give it the signature int main()
.Upvotes: 0