Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Compilation Problem

Compilation Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    #include
    using namespace std;
    template class Array {
    public:
    friend ostream& operator<< (ostream&, Array&);

    private:
    T *pType;
    };

    template
    ostream& operator<< (ostream& output, Array& theArray)
    {
    output << "output array" << endl;
    return output;
    }
    ostream& operator<< (ostream&, Array&);
    int main()
    {
    Array theArray;
    cout << theArray << endl;
    return 0;
    }

    I tried compiling the following code and I get a warning on line 5 that this statement declares a non-template function but despite this warning the code does compile. However, it does not link. The error message from the linker is: /tmp/ccIbnsNk.o:t2.C:(.text+0x9b): undefined reference to `operator<<(std::basic_ostream >&, Array&)' I feel the above code defines the function the linker says is undefined. What am I missing? Bob

    G 1 Reply Last reply
    0
    • B BobInNJ

      #include
      using namespace std;
      template class Array {
      public:
      friend ostream& operator<< (ostream&, Array&);

      private:
      T *pType;
      };

      template
      ostream& operator<< (ostream& output, Array& theArray)
      {
      output << "output array" << endl;
      return output;
      }
      ostream& operator<< (ostream&, Array&);
      int main()
      {
      Array theArray;
      cout << theArray << endl;
      return 0;
      }

      I tried compiling the following code and I get a warning on line 5 that this statement declares a non-template function but despite this warning the code does compile. However, it does not link. The error message from the linker is: /tmp/ccIbnsNk.o:t2.C:(.text+0x9b): undefined reference to `operator<<(std::basic_ostream >&, Array&)' I feel the above code defines the function the linker says is undefined. What am I missing? Bob

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      This line:

      ostream& operator<< (ostream&, Array<int>&);

      declares a new function that takes an Array<int> as an argument, but you haven't provided a function body which leads to the linker error. If you meant to explicitly instantiate the template for int, you need a bit more:

      template ostream& operator<< (ostream&, Array<int>&);

      B 1 Reply Last reply
      0
      • G Graham Breach

        This line:

        ostream& operator<< (ostream&, Array<int>&);

        declares a new function that takes an Array<int> as an argument, but you haven't provided a function body which leads to the linker error. If you meant to explicitly instantiate the template for int, you need a bit more:

        template ostream& operator<< (ostream&, Array<int>&);

        B Offline
        B Offline
        BobInNJ
        wrote on last edited by
        #3

        Thanks for the response. However, I added the line you suggested and it did not solve the problem. I believe the line you suggested tells the compiler that we want to specialize the implementation for the special case of int. However, I am not sure of my facts on this one. Bob

        G 1 Reply Last reply
        0
        • B BobInNJ

          Thanks for the response. However, I added the line you suggested and it did not solve the problem. I believe the line you suggested tells the compiler that we want to specialize the implementation for the special case of int. However, I am not sure of my facts on this one. Bob

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          Sorry, I missed out that you have to fix the friend declaration too:

          template<T>
          friend ostream& operator<< (ostream&, Array<T>&);

          An explicit specialization would look like this:

          template<>
          ostream& operator<< (ostream& output, Array<int>& theArray)
          {
          output << "output int array" << endl;
          return output;
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups