Solved

Linking errors

Posted on 2003-11-13
5
210 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

756 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