undefined reference to function declared in header and implemented in cpp file












0















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









share|improve this question


















  • 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











  • 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
















0















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









share|improve this question


















  • 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











  • 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














0












0








0








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 22:46









OberrukOberruk

83




83








  • 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











  • 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





    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











  • 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












1 Answer
1






active

oldest

votes


















0














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:




  1. In 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).

  2. The return; at the end of the function body is also point-less for a void function.

  3. If main doesn't use the command line arguments, you can also give it the signature int main().






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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:




    1. In 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).

    2. The return; at the end of the function body is also point-less for a void function.

    3. If main doesn't use the command line arguments, you can also give it the signature int main().






    share|improve this answer




























      0














      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:




      1. In 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).

      2. The return; at the end of the function body is also point-less for a void function.

      3. If main doesn't use the command line arguments, you can also give it the signature int main().






      share|improve this answer


























        0












        0








        0







        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:




        1. In 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).

        2. The return; at the end of the function body is also point-less for a void function.

        3. If main doesn't use the command line arguments, you can also give it the signature int main().






        share|improve this answer













        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:




        1. In 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).

        2. The return; at the end of the function body is also point-less for a void function.

        3. If main doesn't use the command line arguments, you can also give it the signature int main().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 23:58









        user10605163user10605163

        2,858624




        2,858624
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks