Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linking errors

Posted on 2003-11-13
5
Medium Priority
?
213 Views
Last Modified: 2010-04-02
when I compile this I get no errors. When I go to debug/run I get these 2 errors, I'm using MS Visual Studio .NET for compiler

#include <iostream>
using namespace std;

double sum (double);
main()
{

double ctr=1;
double x;
double total;
total = sum(ctr);
cout << total;
}

double sum (double &x)
{
      double temp;

      if (x<10)
            temp = sum(x+1)+1/(x*x);
      else
            temp = 1/x;
      return temp;
}


error LNK2001: unresolved external symbol "double __cdecl sum(double)" (?sum@@$$FYANN@Z)
fatal error LNK1120: 1 unresolved externals

thanks for the help with this.....
0
Comment
Question by:Orlando15767
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 320 total points
ID: 9744484
Hi Orlando15767,

First of all

the declaration of the sum function does not match the definition (one receives a reference to a double and the other just a double).
Also the parameter can be passed as a const reference


Try with

#include <iostream>
using namespace std;

double sum (const double &);
main()
{

double ctr=1;
double x;
double total;
total = sum(ctr);
cout << total;
}

double sum (const double &x)
{
     double temp;

     if (x<10)
          temp = sum(x+1)+1/(x*x);
     else
          temp = 1/x;
     return temp;
}

Hope this helps

Tincho
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9744508
works good

if you don't mind, could you elaborate a little bit about const regerence how it works..... thanks for the help
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9744565
Ok

When you pass an object to a function you pass the whole object

void function( MyClass object )

but when you pass a reference to an object you pass just the address of that object (generally smaller than the object itself), so it is often recommended that you pass objects as references.....

void function( MyClass & object )

When you pass a const referente to an object

void function( const MyClass & object )

it means that you'll pass the address of the object, but the object can't be changed.
you can only call const functions to that object or get the member variables, but you cannot modify it.

I guess that's almost everything

If you need any more help let me know

Tincho
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9744638
thanks for the help and the added explaination....... helped me understands what the code was actually doing....

thanks again
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9744655
Glad it helped Orlando

:D

Tincho
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question