Solved

Linking errors

Posted on 2003-11-13
5
205 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
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now