C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2004, 09:19 AM
seamus2 seamus2 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 3 seamus2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
compiling different cpp files on bloodshed developer on a windows platform

I have a question.

It is my understanding that makefiles can be used to link different source files together. This could be useful as it can make classes and functions that refer to them more portable. However I am unclear of how to do this.

Here is an example piece of code the source file containing my main is:

Code:
#include <iostream>

#include "test.h"


using namespace std;

int main()
{
    test t(4.0);
    cout << square(t) << '\n';

}


The header looks like this:

Code:
#ifndef TEST_H
#define TEST_H
class test
{
      private:
      double a_number;

      public:
      test()
      {
            a_number = 0;
      }
      test(double a)
      {
             a_number = a;
      }
      double geta()
      {
             return a_number;
      }
};

double square (test &);
#endif TEST_H


and the source file containing the 'square function' looks like this:

Code:
#include "test.h"

double square (test a)
{
     return a.geta() * a.geta();
}


I automatically generate a makefile with bloodshed devC++ but when I build I still get an error message saying that the main contains an undefined reference to 'square(double &)' .

Thanks for any explanation!

Reply With Quote
  #2  
Old January 30th, 2004, 09:36 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
Function overloading just bit you there.

In C++, you can have several functions all with the exact same name but with different data types in the argument list. It's the different argument lists that makes each same-named function distinquishable from each other and allows the compiler to figure out which one you're calling. There's even a special term, "name mangling", that describes how the compiler appends the argument-list data types to the function name for the linker.

In test.h, you provide this function prototype:
double square (test &);
Yet in the other source file where you think that you're implementing that function, the function's header looks like this:
double square (test a)

You changed the argument-list data types, so it's a completely different function than you had prototyped in the header file. The linker error of "undefined reference to 'square(double &)'" is righteous because you never did implement a "square" function with a reference arguement (though I'm not sure why it's "double &" and not "test &").

You probably did intend the function's argument to be a reference, so just add the & and I think it should work.

Reply With Quote
  #3  
Old January 30th, 2004, 09:57 AM
seamus2 seamus2 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 3 seamus2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Thank you for your clarification. Can't believe that that was all that was wrong!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > compiling different cpp files on bloodshed developer on a windows platform

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap