Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 510359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:06:19+00:00 2026-05-13T07:06:19+00:00

I have a class ‘Vector3’ which is compiled successfully. It contains both non-friend and

  • 0

I have a class ‘Vector3’ which is compiled successfully. It contains both non-friend and friend functions, for example, to overload * and << operators when Vector3 is the second operand. The problem is I can’t link to any of the friend functions, be it operator overloaded or not. So I can confirm that the error is not specific to operator overloading. The g++ command used for linking is as follows (please also see Makefile at the end),

g++ -Wall -W -I./ -g -o main.out main.o Vector3.o

which gave the following errors,

main.cpp:7: undefined reference to `operator*(double, Vector3 const&)'
main.cpp:9: undefined reference to `mag(Vector3 const&)'
main.cpp:10: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Vector3 const&)'

Below is the relevant code in my source files. I follow the practice of making separate .hpp and .cpp for every class.

/* file Vector3.hpp */
struct Vector3 {
    ...
    Vector3 operator*(const double k) const;
    friend Vector3 operator*(const double k, const Vector3 &vec);
    double magnitude() const;
    friend double mag(const Vector3 &vec);
    friend std::ostream& operator<<(std::ostream& output, const Vector3 &vec);
    ...
}

/* file Vector3.cpp */
Vector3 operator*(const double k, const Vector3 &vec) {
    ...
}

inline double mag(const Vector3 &vec) {
    ...
}

std::ostream& operator<<(std::ostream& output, const Vector3 &vec) {
    ...
}

/* file main.cpp */
#include "Vector3.hpp"
int main() {
    Vector3 M(1, 1, 1);
    M = M * 2.0;              // own operator* links successfully
    M = 10.0 * M;             // friend operator* doesn't link
    double m = M.magnitude(); // own function magnitude() links successfully
    double n = mag(M);        // friend function mag() doesn't link
    std::cout << M;           // friend operator<< doesn't link
}

Finally, this is my Makefile.

CXX         = g++
CXXFLAGS    = -Wall -W $(INCPATH) -g
INCPATH     = -I./
OBJS        = main.o Vector3.o

main.out: $(OBJS)
 $(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LIBPATH)

main.o: main.cpp
Vector3.o: Vector3.cpp

clean:
 rm -f $(OBJS) main.out

The strangest thing is that if I include the Vector3.cpp file as well in main.cpp and then remove Vector3.o from OBJS in Makefile, the program links successfully. I cannot make sense of this. Please help me!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:06:20+00:00Added an answer on May 13, 2026 at 7:06 am

    The definition of friend operator* uses fp_type while the friend declaration uses double as the first parameter. This will only work as intended if fp_type is a typedef-name for double. Are you sure fp_type actually stands for double? I can’t see it from the code you posted.

    The problem with mag is rather obvious: you defined it as inline in .cpp file. Inline function definitions have to be visible everywhere they are used, meaning that normally they should be placed in the header file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Class and Student objects. Both have collection of another as property. Which
I have class like this below shown. which contains the shopping items where the
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
We have class lua. In lua class there is a method registerFunc() which is
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
I have class User that contains protected constructor and class Account that has access
I have class Building in which i have one class member NSMutableArray *subnode.I have
I have Class which Extends View I'm able To Move one Image Over another
I have class which has a method that needs to return three DataTables. I
I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.