Thread: Why does the following code not compile

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Why does the following code not compile

    Code:
    #include <iostream>
    #include <fstream>
    #include <set>
    #include <map>
        /*
         * Generates a clause_set and a dictionary, given an inputstream
         * Returns a clause_set
         * Returns a dictionary through the parameter: std::map<std::string, long>
         */
    std::set < std::set < long >>generate(std::ifstream &, std::map < std::string,
                                          long >*);
    
    /*
     * Reprints the given Set provided a dictonary.
     */
    void printClauseSet(std::set < std::set < long > >, std::map < std::string,
                        long >&);
    //Reprints the given Clause
    void printClause(std::set < long >, std::map < std::string, long >&);
    
    int main(int argc, char *argv[])
    {
      if (argc != 2) {
        std::cout << "You must specify exactly one argument." << std::endl;
        return EXIT_FAILURE;
      }
      std::map < std::string, long >dictonary;
      std::ifstream infile(argv[1]);
    
      std::set < std::set < long >>clause_set = generate(infile, &dictonary);
    
    }
    
    std::set < std::set < long >>generate(std::fstream & infile,
                                          std::map < std::string, long >*to_return)
    {                               //stuffhere}

    Compiler Error:

    Undefined symbols for architecture x86_64:
    "generate(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, long, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic _string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, long> > >*)", referenced from:
    _main in main.cpp.o
    ld: symbol(s) not found for architecture x86_64

    Mysterious to me... What has this to do with x86_64
    Last edited by Salem; 02-27-2020 at 11:25 PM. Reason: Removed crayola and disabled smilies

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    It's a link error stating that you didn't define the function named generate which has a first parameter of type std::ifstream&, which is true because you defined an overloaded function named generate that has a first parameter of type std::fstream& instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Quote Originally Posted by laserlight View Post
    It's a link error stating that you didn't define the function named generate which has a first parameter of type std::ifstream&, which is true because you defined an overloaded function named generate that has a first parameter of type std::fstream& instead.
    Is there anything like casting in C++ ?

    In Java you could pass an Object of an Childclass as an object of every upper-class.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    It's the same in C++. You can easily demonstrate this by changing the forward declaration to have an fstream first parameter instead.

    As I said, the issue is that you didn't define the function correctly. So, the compiler generated code that invoked the function according to the forward declaration, but the linker couldn't find the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code won't compile
    By monkles in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 01:45 PM
  2. Code will not compile - code from a book!
    By Chubz in forum C++ Programming
    Replies: 19
    Last Post: 09-12-2004, 10:21 PM
  3. Replies: 3
    Last Post: 03-07-2003, 09:06 PM
  4. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM

Tags for this Thread