Solved

Linking errors

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 clear a vector as well as how to detect empty vectors in C++.

706 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

16 Experts available now in Live!

Get 1:1 Help Now