This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I have a small program I have written that accepts user input and creates a linked list. I am encountering an error when building that seems to relate to the passing of the HeadPointer into the print function. The error is as follows:
undefined reference to `PrintList(PetData*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
If anyone could point(put not intended) me in the right direction or even explicitly give me a fix I would really appreciate it.
Source Code:
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
struct PetData{
int IdNumber;
string PetType;
PetData *link;
};
PetData * HeadPointer=NULL;
void InsertItem(int, string, PetData*);
void PrintList(PetData*);
int main(){
int init_IdNumber;
string init_PetType;
PetData * CRP = NULL;
char cflag = 'Y';
while (toupper(cflag) == 'Y'){
cout << "Please Enter an ID Number: " << endl;
cin >> init_IdNumber;
cout << "Please Enter a Pet Type: " << endl;
cin >> init_PetType;
CRP = new PetData;
InsertItem(init_IdNumber,init_PetType,CRP);
HeadPointer=CRP;
cout << "Would you like to enter another? (Y/N)" << endl;
cin >> cflag;
};
PrintList(HeadPointer);
return 1;
};
void InsertItem(int init_IdNumber, string init_PetType, PetData* CRP){
CRP->PetType = init_PetType;
CRP->IdNumber = init_IdNumber;
CRP->link = HeadPointer;
};
void PrintList(){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "\t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}
};
Before anyone brings it up, this is not a homework assignment. It was something my professor suggested we create independently in preparation for a future midterm assignment.
Thanks!
The function declaration and definiton is not matching.
The prototype of the function is
void PrintList(PetData*); and the definition is void PrintList(). This is why it is not matching.
There is the corrected code
void PrintList(PetData* HeadPointer){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "\t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}
Related
I'm trying to make this kina guess game on C++, I'm a beginner and still learning.
What I'm trying to achieve is a guessing game of two const names, is very simple without errors but it's not working after I enter something. it should report until I find the correct name. Please also I don't want to change the structure of the code, just find why isn't working.
#include <iostream>
#include <string>
using namespace std;
struct Vlerat {
string guess01 = "Resul";
string guess02 = "Rejan";
int numruesi = 0;
};
int main() {
Vlerat funksioni;
string nameGuess;
int nameOkay = 0;
cout << "Gjej njerin prej dy emrava te fshehura." << endl;
cout << "Ndihm: Fillojn me Shkronjen 'R', dhe pas asaj vjen edhe nje shkronj 'e'" << endl;
do {
cout << "Shkruaj Emrin > "; cin >> nameGuess;
if (nameGuess == funksioni.guess01){
cout << "Ju e keni gjetur emrin e njerit nga personat duke provuar gjithesej:";
cout << funksioni.numruesi++ << " here." << endl;
nameOkay++;
}
if (nameGuess == funksioni.guess02) {
cout << "Ju e keni gjetur emrin e njerit nga personat duke provuar gjithesej:";
cout << funksioni.numruesi++ << " here." << endl;
nameOkay++;
}
funksioni.numruesi++;
} while(nameOkay = 0);
}
You should change while(nameOkay = 0); to while(nameOkay == 0);.
Because = is an assignment, but == is operator of comparing (equality)
Read about it here
And here
I am receiving the following debug error when attempting to run the first part of my program:
Debug Error!
Program:
...\user\desktop\PunchLineProgram\Debug\PunchLineProgram.exe
Module:
...\user\desktop\PunchLineProgram\Debug\PunchLineProgram.exe
File:
Run-Time Check Failure #3 - T
(Press Retry to debug the application)
I am attempting to have the user select whether they want to hear a joke, running and if\else statement that will output a message to the user, based on their response. If I comment out these statements, I do not receive the error when attempting to run the program. I know I'm probably missing something simple, as I am a novice. Here is the code that I have so far:
/*Include Section*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
/*Namespace Section*/
using namespace std;
/*Function Prototypes Section*/
void displayAllLines(ifstream &infile);
void displayLastLine(ifstream &infile);
/*Main section: this is the entry point of the program, which controls the flow of execution*/
int main()
{
string file1;
string file2;
ifstream joke;
ifstream punchline;
int decision;
char y;
char n;
cout << "*******************************************************************************" << endl;
cout << setw(48) << "Punchline Program" << endl;
cout << "*******************************************************************************" << endl;
cout << endl;
cout << "Welcome to the Punchline Program!" << endl;
cout << "Are you ready to hear a joke? (y or n): ";
cin >> decision;
if (decision == y)
{
cout << "Great! Let's get started!" << endl;
}
else if (decision == n)
{
cout << "Ah, no sense of humor, I see. Time to make like a tree and leaf (queue rimshot)!" << endl;
}
system("PAUSE");
}
Any help would be greatly appreciated!
When comparing to char you should use '':
char answer
if (answer == 'y') { *//this only checks for LOWER case y*
cout << "You selected Yes" << endl;
}
when comparing to a string use ""
int/float/double... you can just use the variable.
Besides that, your decision variable as int when it should be char, and you don't need char y nor n. (you yourself never even used it in that code)
I'd suggest looking up c++ tutorials, most show and explain the different between char/string, ' and ".
I do not know how to declare "random" in the parentheses for "int main()," and need help. (I am a beginner in C++)
Please take a look at my code, try it out, and please notify me with an answer when you think you know how to solve this problem. It'd mean a lot to me. Thanks! Meanwhile, I will keep trying to solve the problem myself as well.
Note: I am using Code::Blocks if you want to be specific.
The error is on Line 7/9 of my code.
Here is my updated code below:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int rn = random() % 21; // generates a random int from 0 to 20
// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;
while (u != rn) // Calculates the answer that you give
{
// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
cout << "You guessed too small!" << endl;
}
// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;
}
// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}
Here's the updated compiler error:
||=== Build: Debug in Guess The Number (compiler: GNU GCC Compiler) ===|
C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp||In function 'int main()':|
C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp|12|
error: 'randomize' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Remove the semicolon near main, the compiler is telling you exactly what the issue is:
int main ();
Should be
int main ()
Your code will also not compile even after fixing this because you have not declared the std namespace. You can put this line at the top for now using namespace std; but it is bad practice. You should declare it manually using the scope resolution operator.
And a number of other issues as already mentioned in the comments above, make sure to read the compiler output thoroughly because it tells you what line is causing the issue.
Your code should look like:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int rn = random() % 21; // generates a random int from 0 to 20
// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;
while (u != rn) // Calculates the answer that you give
{
// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
cout << "You guessed too small!" << endl;
}
// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;
}
// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}
Someone else got to it. There are no semicolons after signatures to methods like main().
One other thing not mentioned, I'm guessing you want
while (u != rn)
Also, be careful of the difference in "=" and "==".
BTW -- Welcome to C++!!!
a little more portable version (doesn't use conio.h) which lets the computer play against himself:
#include <iostream>
#include <cstdlib>
#include <ctime>
int get_random_in_range(int min, int max)
{
return std::rand() % (max - min) + min;
}
// returns 0 if user guessed right, negative value if user
// guessed too small, positive if user guessed too big
int check_user_guess(int guess, int my_secret)
{
return guess - my_secret;
}
int main ()
{
int my_guess = get_random_in_range(1, 10);
std::cout << "I think of " << my_guess << std::endl;
std::cout << "Please guess my number: ";
int user_guess = get_random_in_range(1, 10);
std::cout << user_guess << std::endl;
while (check_user_guess(user_guess, my_guess) != 0)
{
std::cout << "You guessed " << user_guess << std::endl;
if (check_user_guess(user_guess, my_guess) > 0)
{
std::cout << "You guessed too big!" << std::endl;
}
else if (check_user_guess(user_guess, my_guess) < 0)
{
std::cout << "You guessed too small!" << std::endl;
}
std::cout << "Please guess again: ";
user_guess = get_random_in_range(1, 10);
std::cout << user_guess << std::endl;
}
std::cout << std::endl << "You guessed it right!";
}
try it here: http://coliru.stacked-crooked.com/a/5bf0b9201ef57529
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am writing code of ACM problem in which we have to check possibilities of different items. It's some minor error in the code.
#include<iostream>
using namespace std;
void CheckPossibilities( int numItems, int maxWeights )
{
if( numItems <= 0 )
{
cout << "Invalid Items";
}
if ( maxWeights <= 0 )
{
cout << "Impossible";
}
while( maxWeights > 0 )
{
if(numItems%2==0) //for even
{
numItems = numItems / 2;
maxWeights--;
}
else
{
numItems = (numItems -1)/ 2; //for odd
maxWeights--;
}
}
if( numItems <= 1 )
{
cout << "Possible";
}
else
{
cout << "Impossible";
}
}
void main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1 "AND" maxWeights1<<endl;
cout<<CheckPossibilities(numItems1, maxWeights1);
}
Your mistakes were trying to cout multiple strings in one line without concatting them in some way, either seperate with a << or a +. You also cant cout a void function because it tries to output void, you just need to call it and let the function do the outputting. With errors fixed the main should be
int main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1+"AND"+maxWeights1<<endl;
CheckPossibilities(numItems1, maxWeights1);
return 0;
}
Next time look at what line the error is thrown on when compiling and search those specific error because these were really simple and specific syntax errors that could be found by a google search easily.
void main()
{
int numItems1, maxWeights1;
cout << "enter numItems" << endl;
cin >> numItems1;
cout << "maxWeights" << endl;
cin >> maxWeights1;
cout << numItems1 << "AND" << maxWeights1 << endl;
CheckPossibilities(numItems1, maxWeights1);
}
you can never do this: cout<<CheckPossibilities(numItems1, maxWeights1); cout take standard output stream, not functions. And also you forgot to put << in cout << numItems1 << "AND" << maxWeights1 << endl; in this form, your code build succesful.
I've had a lot of problems with this code and i've fixed most of them but i dont know whats wrong here.
1>------ Build started: Project: molar mass, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(54): error C3867: 'std::basic_ifstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ifstream<char,std::char_traits<char>>::close' to create a pointer to member
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(55): error C3867: 'std::basic_ofstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ofstream<char,std::char_traits<char>>::close' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
That is the build output for visual studio 2013
#include <iostream>
#include <iomanip>
#include <fstream>
#include <Windows.h>
using std::basic_ofstream;//Thought it would help
using namespace std;
using std::basic_ifstream;//Thought it would help
using std::char_traits;//Thought it would help
/**
* Author: Alex M.
* Date: 3/12/2015
* Desc: Calculates the molar
* mass of compounds,
* elements and
* substances.
*/
void nor();
void tut();
void calc();
int i;
double m;
ifstream inFile;//Is this how to do if/ofstream?
ofstream outFile;
int main()
{
char ele;
bool a;
a = true;
if (i<1)
tut();
nor();
while (a = true)
{
cout << "Enter your compound or type 'Help': ";
cin >> ele;
calc();
while (ele != '\n')//This part is unfinished but seems to work
{
}
}
system("pause");
}
void nor()//This is where i think the problem is
{
inFile.open("runs.dat");
outFile.open("runs.dat");
inFile >> i;
i++;
outFile << i;
inFile.close;
outFile.close;
}
void tut()
{
cout << "Enter your equation with each " << endl << "element seperated by a space." << endl;
cout << "Example: HCl -> H Cl" << endl;
cout << "If theres more than one ion of each " << endl << "element per equation, enter that " << endl << "ion as many time as it appears." << endl;
cout << "Example: NaSO4 -> Na S O O O O" << endl;
system("pause");
}
I've already looked all over forums but no one else seems to be having my problem.
I commented all over the code with where i think the problem is.
I'm very new to C so please don't judge my simplistic coding skills.
In the error message where it says:
source.cpp(54)
source.cpp(55)
that means those errors are on line 54 and 55 respectively. You'll find that those lines are:
inFile.close;
outFile.close;
You probably meant to call those functions:
inFile.close();
outFile.close();
It's an error to mention the name of a member function in that way if you are not calling the function (and function calls require parentheses).
However, a better design would be to make inFile and outFile be local objects inside the function where you are using them. Then the files will automatically be closed when those objects go out of scope.