Solved

Linking errors

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 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