Command prompt undefined reference to everything - c++

I am trying to build an application which takes the users input, puts it into a vector/database, and outputs the results. When I input the code into my windows command prompt compiler it just get it gives me a path C:/users/app/data/local/temp and then an undefined reference error to practically everything,cin,cout etc. It was initially only giving me the usual errors, but when I removed the errors it gave me this, whereas if I was to remove a semi-colon it would give me just one error. Any ideas on what might be causing this??
MP3.H
#define MP3_H
#include <iostream>
#include<string>
using namespace std;
class MP3
{
public:
MP3();//constructor
MP3(string,string,int,int,string);
~MP3();
string getName() const;
string getSongName() const;
int getDuration() const;
int getReleaseYear() const;
string getReview() const;
private:
string artistMP;
string songMP;
int duationMP;
int releaseyearMP;
string reviewMP;
}
#endif
MP3.cpp
#include "MP3.h"
MP3::MP3();
MP3::MP3(string artist,string song,int duration,int releaseyear,string review){
artistMP=artist;
songMP=song;
durationMP=duration;
releaseyearMP=releaseyear;
reviewMP=review;
}
~MP3::MP3();
string MP3::getName()const;{
return artistMP;
}
string MP3::getSongName()const;{
return songMP;
}
int MP3::getDuration()const;{
return durationMP;
}
int MP3::getReleaseYear()const;{
return releaseyearMP;
}
string MP3::getReview()const;{
return reviewMP;
}
main.cpp
#include<vector>
#include "MP3.h"
void fillVector(vector<MP3>&);//5
void print (const vector<MP3>&);//7
int main( )
{
vector<MP3> myRecord;
fillVector(myRecord);
print(myRecord);
return 0;
}
void fillVector(vector<MP3> & newMyRecord){//22
cout<<"How many songs in the playlist baby";
int recordSize;
cin>>recordSize;
for(int i=0;i<recordSize;i++)
{
string artist;
string song;
int duration;
int releaseyear;
string review;
cout<<"Enter Artist Name:";
cin>>artist;
cout<<"Enter Song Name:";
cin>>song;
cout<<"Enter Song Duration(in seconds):";
cin>>duration;
cout<<"Enter Release Year:";
cin>>releaseyear;
cout<<"Enter Brief Review:";
cin:review;
MP3 newMP3(artist,song,duration,releaseyear,review);
newMyRecord.push_back(newMP3);
cout<<endl;
}
cout<<endl;
}
void print (const vector<MP3>&newMyRecord)//58
{
unsigned int size = newMyRecord.size();
for (unsigned int i=0;i<size;i++){
cout<<"Artist:"<< newMyRecord[i].getName()<<endl;
cout<<"SongName:"<< newMyRecord[i].getSongName()<<endl;
cout<<"Duration:"<< newMyRecord[i].getDuration()<<endl;
cout<<"ReleaseYear:"<< newMyRecord[i].getReleaseYear()<<endl;
cout<<"Review:"<< newMyRecord[i].getReview()<<endl;
}
}
};
TIA

Building a program occurs in (at least) two stages, compilation and linking. Undefined references are linker errors. If you get linker error it implies that compilation was successful. But if you have compiler errors (e.g. bad syntax to do with semi-colons) then linking doesn't happen so you don't see any linker errors (like undefined references).

Related

Unexpected termination of of c++ program

My error in the following program is: It terminates in the middle of execution and does not even allows user to enter the inputs.
#include<iostream>
using namespace std;
class Graph
{
public:
char city;
int distance;
Graph *next;
};
Graph **graph=new Graph*[20];
int size;
void create()
{
cout<<"Enter the number of cities:";
cin>>size;
cout<<"Enter "<<size<<" cities\n";
for(int i=0;i<size;i++)
{
cout<<"City number "<<i+1<<":";
cin>>graph[i]->city;
cout<<endl;
}
}
int main()
{
create();
return 0;
}
The output statements do not even appear and the program terminates. Please can someone run this code and help me out. This issue has never occurred before and so I do not know how to solve this.
Thank you in advance.

I am getting an error " [Error] ld returned 1 exit status" in C++ . Help me with this

I am making a program in C++ in DevC++ for finding missing number from 1 to nth digits but now I am getting an error:
[Error] ld returned 1 exit status.
You can see my program and help me with this.
This program is now valid for 100 digits only.
#include <iostream>
using namespace std;
class miss_number
{
public:
static int arr[100];
int miss;
int n;
public:
void displayBoard();
void getInput();
void findMissing();
void displayMissing();
};
void miss_number :: displayBoard()
{
cout<<"The program for Finding Missing Number Between 1-100 digit";
}
void miss_number :: getInput()
{
int i;
cout<<"\nEnter how many digits :";
cin>>n;
cout<<"\nEnter the number between 1-100 digit :";
for(i=0;i<n-1;i++)
{
cin>>arr[i];
}
}
void miss_number :: findMissing()
{
int i,total,totalOfArray=0;
total = n*(n+1)/2;
for(i=0;i<n-1;i++)
{
totalOfArray = totalOfArray + arr[i];
}
miss = total - totalOfArray;
}
void miss_number :: displayMissing()
{
cout<<"\nMissing number :"<<miss;
}
int main()
{
miss_number o;
o.displayBoard();
o.getInput();
o.findMissing();
o.displayMissing();
return 0;
}
The error comes from the declaration
static int arr[100];
Since you declared it as static, you must define it outside the class:
int miss_number::arr[100];
This way, the program compiles and links. If using C++17, you can also just declare it as inline static and the effect would be the same.
If you just remove the static in your declaration without adding anything else, it also compiles, and it should be the correct way to do this in your case, because arr is a member of the instanced object. This way, you'd be able to create multiple instances of the class and they'd each have its own arr.
With that said, you don't need to return arr in getInput and then repass it to findMissing, since it's visible from inside all member functions of the class. Just removing all parameters from findMissing in all instances fixes this.

C++ code compiles but doesn't run [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently writing a Texas Hold'em code in order to learn more about c++ and gain experience. But I recently ran into a problem in which I have no idea what to do.My code compiles just fine without errors but once I make it run and it arrive at a specific function it just stops working (as in I get an error from CodeBlock saying your program has stopped working). I have tried cutting out parts such as loops in the function to see which specific part is the problem but after a couple of days im still at a stop.
Here is the function and class that I believe is the problem:
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
public:
void set_name(string a){name=a;}
string print_name(){return name;}
void card_generator();
string set_cards(int c){return cards[c];}
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
int p=0,k;
while(p<6){
k=0;
final_card[0][k]=tempCards[p];
k++;
p++;
}
while(p<12){
k=0;
final_card[1][k]=tempCards[p];
k++;
p++;
}
while(p<17){
k=0;
table_cards[k]=tempCards[p];
k++;
p++;
}
}
Here is the full code in case I am wrong of the source of the problem:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
using namespace std;
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
public:
void set_name(string a){name=a;}
string print_name(){return name;}
void card_generator();
string set_cards(int c){return cards[c];}
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
string Player::cards[53];
string Player::final_card[2][6];
string Player::table_cards[5];
int main () {
int choice1=0, i, max_i, tempV;
string username;
cout<< "Welcome to Texas Hold'Em!\n1-Play\n2-Quit\n";//menu
while((choice1!=1)&&(choice1!=2)){//Makes sure that user enters correct input```
cin>>choice1;
if ((choice1!=1)&&(choice1!=2)){
cout<<"Invalid Input!\nTry again!\n";
}
}
system ("cls");
if (choice1==2){//End Program
return 0;
}
cout<<"How many players?[2-6]"<<endl;
while((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){//Makes sure that user enters correct input
cin>>i;
if ((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){
cout<<"Invalid Input!\nTry again!\n";
}
}
Player player[i];//creating array of players
player[0].card_generator();
max_i = i;//max_i is nb of players
i--;//since arrays start at 0
system("cls");
player[0].set_final_card(i,max_i);
player[0].print_cards();
if (choice1==1) {//SET NAMES OF ALL PLAYERS
for(i=0; i<max_i; i++){
cout<< "Whats your name?\n";
cin>>username;
player[i].set_name(username);
cout<<"Your name is "<< player[i].print_name()<< " and you have "<< player[i].print_bank()<<"$\n";
tempV=i+1;//used bc arrays start at 0
if(tempV!=max_i){
cout<< "Give PC to player "<< i+2 <<endl;
}
_sleep(3000);
system("cls");
}
}
return 0;
}
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
int p=0,k;
while(p<6){
k=0;
final_card[0][k]=tempCards[p];
k++;
p++;
}
while(p<12){
k=0;
final_card[1][k]=tempCards[p];
k++;
p++;
}
while(p<17){
k=0;
table_cards[k]=tempCards[p];
k++;
p++;
}
}
void Player::card_generator(){
string card_value[13];
card_value[0]="1";
card_value[1]="2";
card_value[2]="3";
card_value[3]="4";
card_value[4]="5";
card_value[5]="6";
card_value[6]="7";
card_value[7]="8";
card_value[8]="9";
card_value[9]="10";
card_value[10]="J";
card_value[11]="Q";
card_value[12]="K";
string card_type[4];
card_type[0]="of hearts";
card_type[1]="of diamonds";
card_type[2]="of clubs";
card_type[3]="of spades";
string card[53];
int x=0;
fill_n(card,53,0);
for (int j=0;j<4;j++){
for (int q=0;q<13;q++){
card[x]=card_value[q]+" "+card_type[j];
cards[x]=card[x];
x++;
}
}
}
If you have any criticism about the code itself even if not directly linked to problem feel free to tell me as I'm doing this to learn :D. Thank you in advance!!
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
Be consistent in what you do. Including <stdlib.h> and <ctime> looks strange. Either include <cstdlib> and <ctime>, or include <stdlib.h> and <time.h>.
using namespace std;
Don't do this. This using imports all names from the std namespace, which is several hundreds. Only import those names that you actually need, or, alternatively, write std::time instead of the unqualified time. This makes it perfectly clear that you are referring to the time from the standard library instead of one that you might have defined yourself.
class Player{
string name;
int bank=100;
static string cards[53];
static string final_card[2][6];
static string table_cards[5];
The cards should not be represented as strings, but as a separate data type called Card, with properties like suit and rank and a to_string method.
public:
void set_name(string a){name=a;}
To make your program fast, pass a as const std::string & instead of a simple string. This will prevent some copying of data. You should give a better name to the parameter, e.g. void set_name(const std::string &name) { this.name = name; }.
string print_name(){return name;}
This method does not print anything, therefore it must not be called print_name.
void card_generator();
Methods usually are named with verbs, not with nouns. So generate_cards would be a better name. But what does generate mean here? (I'm not a native English speaker, but would draw_cards describe it accurately?)
string set_cards(int c){return cards[c];}
A method called set_* usually modifies something. This one doesn't. Why did you name it this way?
int print_bank(){return bank;}
void set_final_card(int i, int max_i);
Give better names to the parameters. From reading only this declaration, I have no idea what i and max_i might mean.
void print_cards(){for(int i=0;i<6;i++){cout<<final_card[0][i]<<endl;}}
};
string Player::cards[53];
string Player::final_card[2][6];
string Player::table_cards[5];
It looks strange that the cards are stored in the Player class, since no poker player should ever have insight to all 52 cards. And why 53? Is there a joker in your game? These three fields should be moved to a class Table. This allows you to have multiple independent tables, which is nice for a big tournament.
int main () {
int choice1=0, i, max_i, tempV;
string username;
cout<< "Welcome to Texas Hold'Em!\n1-Play\n2-Quit\n";//menu
while((choice1!=1)&&(choice1!=2)){//Makes sure that user enters correct input```
Before reading the choice1 variable, you must initialize it. Since you don't do it, you invoke undefined behavior and everything that the program does after that is unpredictable.
cin>>choice1;
if ((choice1!=1)&&(choice1!=2)){
cout<<"Invalid Input!\nTry again!\n";
}
}
system ("cls");
if (choice1==2){//End Program
return 0;
}
cout<<"How many players?[2-6]"<<endl;
while((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){//Makes sure that user enters correct input
Same here. The user hasn't yet entered anything, so how can you check it?
cin>>i;
Add error handling for every input by enclosing it in an if clause: if (std::cin >> i) {.
if ((i!=2)&&(i!=3)&&(i!=4)&&(i!=5)&&(i!=6)){
cout<<"Invalid Input!\nTry again!\n";
}
}
Player player[i];//creating array of players
Don't use arrays, use a std::vector instead. This allows you to easily extend the table to have 10 players. In the end, there should not be a single 6 in your program.
player[0].card_generator();
max_i = i;//max_i is nb of players
Why do you call this variable max_i, when the comment says that max_players would be a better name?
i--;//since arrays start at 0
system("cls");
player[0].set_final_card(i,max_i);
player[0].print_cards();
if (choice1==1) {//SET NAMES OF ALL PLAYERS
for(i=0; i<max_i; i++){
cout<< "Whats your name?\n";
cin>>username;
player[i].set_name(username);
cout<<"Your name is "<< player[i].print_name()<< " and you have "<< player[i].print_bank()<<"$\n";
tempV=i+1;//used bc arrays start at 0
if(tempV!=max_i){
What does the V in tempV mean?
cout<< "Give PC to player "<< i+2 <<endl;
}
_sleep(3000);
system("cls");
}
}
return 0;
}
void Player::set_final_card(int i, int max_i){
srand(time(NULL));
int tempV = 17;//create temp cards
This 17 is a magic number. It would be better to write it as 5 + 6 * 2, since that makes it much clearer.
string tempCards[tempV];
int randNB[tempV];
int check1 = 0, tmp;
while (check1==0){
for(int g=0; g<tempV;g++){
tempCards[g]=cards[rand()%53];
The 53 is wrong here. I can only be wrong. When you select from 52 cards with equal probability, it must be % 52.
check1=1;
tmp = g - 1;
for(int o=tmp; o!=0; o--){
if (tempCards[g]==tempCards[o]){
check1=0;
}
}
}
}
Captain Giraffe has answered this question in the comments. If any newbies like me face a similar problem look up what a debugger is, as errors like these are called run-time errors. Check this page for a simple explanation: http://www.cplusplus.com/forum/articles/28767/ .

How to debug a C++ program using arrays?

#include<iostream.h>
#include<conio.h>
class XIIB
{
char name[30];
int age;
int roll;
float marks;
public:
void getdata(char a[30],int i,int j,float k)
{
name[30]=a[30];
age=i;
roll=j;
marks=k;
}
void putdata(void)
{
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Roll:"<<roll<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
const int size=5;
XIIB student[size];
void main()
{
char x[30];
int ag;
int rno;
float mrks;
for(int p=0;p<size;p++)
{
cout<<"Enter Name,Age,Roll and Marks of Student"<<p+1<<endl;
cin>>x>>ag>>rno>>mrks;
student[p].getdata(x,ag,rno,mrks);
}
for(p=0;p<size;p++)
{
cout<<"Student"<<p+1<<endl;
student[p].putdata();
}
getch();
}
This program compiles without any error. Also takes the input for Name, Roll No, Age and Marks as expected but it is unable to display the name of the Students. I seem to have made some error in the getdata or putdata functions.
In void getdata(char a[30],int i,int j,float k): name[30]=a[30]; does not make what you expect and is undefined behavior; (this copies the 31st character of a to the 31st character of name, while both strings are only 30 characters long.)
You have to replace name[30]=a[30]; by strcpy(name,a); and this should work.
your getdata has a problem, character arrays cannot be copied like that, try this
void getdata(char a[30],int i,int j,float k)
{
strcpy(name,a); //change this line
age=i;
roll=j;
marks=k;
}
instead of
name[30]=a[30];

How to pass array of input from an external file into main program?

I am new to C++ and i'm having trouble with my program using classes and inputfile to display my input in the output. How should I display the country, population and area? I'm getting error messages like:
Line 82 [Error] invalid use of 'Country::Country'
Line 89 [Error] invalid types 'long int[int]' for array subscript
Line 93 [Error] invalid types 'double[int]' for array subscript
Here is what I have so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Country
{
private:
string name;
long int population;
double area;
public:
Country();
Country(string, long, double);
void setName(string);
void setPopulation(long);
void setArea(double);
string getName();
long getPopulation();
double getArea();
};
Country::Country(){
name="?";
population=0;
area=0;
}
Country::Country(string name1, long population1, double area1){
name=name1;
population=population1;
area=area1;
}
void Country::setName(string name1){
name=name1;
}
void Country::setPopulation(long population1){
if(population1>=0.0)
population=population1;
else{
population1=0.0;
cout<< "Invalid number. Setting population to 0."<<endl;
}
}
void Country::setArea(double area1)
{
if(area1>=0.0)
area=area1;
else{
area1=0.0;
cout<< "Invalid number. Setting area to 0."<<endl;
}
}
string Country::getName(){
return name;
}
long Country::getPopulation(){
return population;
}
double Country::getArea(){
return area;
}
int main(){
Country home;
const int H=5;
string homename="";
long homepopulation=0;
double homearea=0;
ifstream infile("mycountrydata.txt");
home.setName(homename);
home.setPopulation(homepopulation);
home.setArea(homearea);
home.Country(homename, homepopulation, homearea);
for(int i=0; i<H; i++){
cout<<"Enter the country's name: ";
infile>>homename[i];
cout<<endl;
cout<<"Enter the country's population: ";
infile>>homepopulation[i];
cout<<endl;
cout<<"Enter the country's area: ";
cout<<endl;
infile>>homearea[i];
}
infile.close();
return 0;
}
A constructor is a special member function which cannot be directly invoked this way:
home.Country(homename, homepopulation, homearea);
long's don't have the [] operator defined and hence you can't do:
infile>>homepopulation[i];
since earlier you declare long homepopulation. The same explanation holds for the error in
infile>>homearea[i];
These are answers addressing the exact errors in your code, but it isn't a substitute for a good teaching resource. See this answer for some useful material.
country is a constructor and it can be invoked by giving the below statement in the beginning of the main() replacing country home;
country home(homename, homepopulation, homearea);
I guess you want to use homepopulation and homearea as arrays but you declared them as normal variables.