trying to figure this out for a basic program

U

urbman

I am trying to alter a few functions in this basic code, but I am
stuck. I am getting these errors. Any help you can provide would be
greatly appreciated. Thanks.

c:\documents and settings\urbanp\desktop
\cps130\module1\urban1\urban1.cpp(24) : warning C4700: uninitialized
local variable 'len' used
Linking...
urban1.obj : error LNK2019: unresolved external symbol "void __cdecl
getname(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)" (?getname@@YAXV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
referenced in function _main
C:\Documents and Settings\urbanp\Desktop\CPS130\module1\urban1\Debug
\urban1.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\urbanp\Desktop
\CPS130\module1\urban1\Debug\BuildLog.htm"
urban1 - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

It's sort of pissing me off because I normally can figure these out.
My code looks like this:

// Program to Demonstrate Functions that Return One Value
// Filename: fun8.cpp

// Replace the getinfo function with two separate functions
// One should getname and another to getlength
// Save this file with the new changes as fun9.cpp

#include <iostream>
#include <string>
#include<string.h>

using namespace std;

void getname(string name);
void getlength(string name, int len);
void display(string name, int len);
void displaystars(int len);

int main()
{
string name;
int len;
getname(name);
display(name, len);
return 0;
}

void getname(string & name, int & len)
{
cout << "Enter your name : ";
getline(cin,name);
getlength(name, len);
}
void getlength(string name, int len)
{
len = name.length();
}
void display(string name, int len)
{
displaystars(len);
cout << name << endl;
displaystars(len);
}

void displaystars(int len)
{
for (int i = 1; i <= len; i++)
cout << '*';
cout << endl;
}
 
R

red floyd

I am trying to alter a few functions in this basic code, but I am
stuck. I am getting these errors. Any help you can provide would be
greatly appreciated. Thanks.

c:\documents and settings\urbanp\desktop
\cps130\module1\urban1\urban1.cpp(24) : warning C4700: uninitialized
local variable 'len' used
Linking...
urban1.obj : error LNK2019: unresolved external symbol "void __cdecl
getname(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)" (?getname@@YAXV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
referenced in function _main
C:\Documents and Settings\urbanp\Desktop\CPS130\module1\urban1\Debug
\urban1.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\urbanp\Desktop
\CPS130\module1\urban1\Debug\BuildLog.htm"
urban1 - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Look at the prototype for getname, and then look at the actual
implementation for getname.

Also, your getlength function will not do what you think it does.
 
K

Kai-Uwe Bux

(e-mail address removed) wrote:

[snip]
Linking...
urban1.obj : error LNK2019: unresolved external symbol "void __cdecl
getname(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)" (?getname@@YAXV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
referenced in function _main [snip]

Declaration:
void getname(string name); [snip]

int main()
{
string name;
int len;
Use:
getname(name);
display(name, len);
return 0;
}

Definition:
void getname(string & name, int & len)
{ [snip]
}
[snip]

You don't define what you declare. You define something else. The number of
arguments is not the same. Also, instead of a string parameter, you use a
string& parameter. For that reason, the function that you declare and use
is never defined and the linker complains.


Best

Kai-Uwe Bux
 
R

redkrzy

[snip]
void getname(string name);
void getlength(string name, int len);
void display(string name, int len);
void displaystars(int len);

[snip]
you don't need to pass or retrieve length of the string when using the
STL string or other implied string classes, almost all the STL class
implementation have length implemented.
Hence use them instead directly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top