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

  • Home
  • SEARCH
  • 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 764965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:48:49+00:00 2026-05-14T16:48:49+00:00

I have two C++ files, say file1.cpp and file2.cpp as //file1.cpp #include<cstdio> void fun(int

  • 0

I have two C++ files, say file1.cpp and file2.cpp as

//file1.cpp  
#include<cstdio>  
void fun(int i)  
{  
   printf("%d\n",i);  
}

//file2.cpp
void fun(double);
int main()
{
   fun(5);
}  

When I compile them and link them as c++ files, I get an error “undefined reference to fun(double)”.
But when I do this as C files, I don’t get error and 0 is printed instead of 5.
Please explain the reason.
Moreover I want to ask whether we need to declare a function before defining it because
I haven’t declared it in file1.cpp but no error comes in compilation.

  • 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-14T16:48:50+00:00Added an answer on May 14, 2026 at 4:48 pm

    This is most likely because of function overloading. When compiling with C, the call to fun(double) is translated into a call to the assembly function _fun, which will be linked in at a later stage. The actual definition also has the assembly name _fun, even though it takes an int instead of a double, and the linker will merrily use this when fun(double) is called.

    C++ on the other hand mangles the assembly names, so you’ll get something like _fun@int for fun(int) and _fun@double for fun(double), in order for overloading to work. The linker will see these have different names and spurt out an error that it can’t find the definition for fun(double).

    For your second question it is always a good idea to declare function prototypes, generally done in a header, especially if the function is used in multiple files. There should be a warning option for missing prototypes in your compiler, gcc uses -Wmissing-prototypes. Your code would be better if set up like

    // file1.hpp
    #ifndef FILE1_HPP
    #define FILE1_HPP
    void fun(int)
    #endif
    
    // file1.c
    #include "file1.hpp"
    ...
    
    // file2.c
    #include "file1.hpp"
    int main()
    {
        fun(5);
    }
    

    You’d then not have multiple conflicting prototypes in your program.

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the problem is that you are missing a… May 16, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer I would recommend starting with Rails 2.3.8 if this is… May 16, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer Battlefield 2 uses the Gamespy 3 protocol. Source: GameQ library… May 16, 2026 at 6:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Lets say I have files: Libs: one.cpp, one.h two.cpp, two.h three.cpp, three.h Program: program.cpp
Lets say a directory has two files. Here are the contents File1.txt tagstart random
Let's say I have two files.. I want to compare them side-by-side and see
I have two files. main.py that has main program logic and functions.py that has
Suppose that you have two huge files (several GB) that you want to concatenate
I'm stumped! In PHP in Netbeans (6.8), a project has two files, file1.php and
Okay so I have two png files, a circle and a maze. Basically the
I have two branch (let say branch A and branch B)in my svn repository
I have two lists (list1 and list2) containing references to some objects, where some
I am trying to compare two .caf files on the basis of some parameter,

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.