I am writingthat code but gives me undefined reference error. Here is the error : undefined ref. yo display students , undefined reference to adCourse
#include <iostream>
#include "Course.h"
#include "Student.h"
#include "StudentReviewSystem.h"
using namespace std;
int main()
{
// create GradeBook object
Course s( 201, "CS");
cout << "name : " << s.courseName << endl;
cout << "ID : " << s.courseID << endl;
cout << "Enter student ID :" << endl;
int ID;
cin >> ID;
cout << "Enter student name :" << endl;
string name;
cin >> name;
s.addStudent(ID,name);
// cout << "You add the student with name: " << s.studentName << " and ID : " <<
cout << "Enter student ID :" << endl;
int ID2;
cin >> ID2;
cout << "Enter student name :" << endl;
string name2;
cin >> name2;
s.addStudent(ID2,name2);
s.displayStudents();
StudentReviewSystem ilknur;
ilknur.addCourse(111,"HUM");
cout << ilknur.courses[0].courseID;
return 0; // indicate successful termination
}
The Linker is telling you that your files has an external dependency it cannot resolve.
Probably you need to include the path to the soruce code (StudentReviewSystem.c) to your project paths. That's how you would solve it in Eclipe. VS adds everything it need when you add files to the project.
Every cpp file has to be compiled and linked in order to get an executeable. (If no external libraries are used this should do at least.)
If that hint does not help, please post your compiler and linker commands or the whole bild output.
Related
The code is supposed to take the class from the .h file and use it in the main to create a custom pet synopsis that can be stored later in another text file. I haven't made the modular extraction to a text file yet because I need to get it at least working and able to actually compile and return the different arrays that make up the custom pet synopsis.
/usr/bin/ld: /tmp/: in function `__static_initialization_and_destruction_0(int, int)':
program3.cpp:(.text+0x411): undefined reference to `dog_list::dog_list()'
/usr/bin/ld: program3.cpp:(.text+0x426): undefined reference to `dog_list::~dog_list()'
collect2: error: ld returned 1 exit status
My .h file
#include<iostream>
#include<cstring>
#include<cctype>
#include<fstream>
using namespace std;
const int SIZE = 20;
const int END = 11;
class dog_list
{
public:
dog_list();
~dog_list();
void record_pets();
private:
char name[SIZE];
char breed[SIZE];
char species[SIZE];
char service[SIZE];
char special[SIZE];
};
dog_list op;
void record_pets();
my main .cpp file
#include "program3.h"
int main()
{
op.record_pets();
return 0;
}
void dog_list::record_pets()
{
char personal_list[SIZE];
int i = 0;
char again;
do
{
cout << "Enter in pets name: ";
cin.get(op.name,25,'\n');
cin.ignore(100,'\n');
cout << endl << "Enter breed of pet: ";
cin.get(op.breed, 25, '\n');
cin.ignore(100,'\n');
cout << endl << "Enter species: ";
cin.get(op.species,25,'\n');
cin.ignore(100,'\n');
cout << endl << "Enter in service qualifications: ";
cin.get(op.service,25,'\n');
cin.ignore(100,'\n');
cout << endl << "Enter in special notes: ";
cin.get(op.special,25,'\n');
cin.ignore(100,'\n');
cout << endl;
cout << "Name: " << op.name << endl;
cout <<"Breed: " << op.breed << endl;
cout << "Species: " << op.species << endl;
cout << "Service Qualifications: " << op.service << endl;
cout << "Special Notes: " << op.special << endl;
cout << "Pet saved! Would you like to enter another pet? Y/N: " << endl;
cin >> again;
cin.ignore(100,'\n');
cout << endl;
if(again == 'y')
{
again = toupper(again);
}
}while(again == 'Y' && i <= END);
{
++i;
}
}
You never implement the constructor and destructor, you just declare it:
dog_list();
~dog_list();
You have to implement it, for example, in main.cpp:
dog_list::dog_list() = default;
dog_list::~dog_list() = default;
I'm having trouble with a simple code I have to for class, I'm trying to figure out how to add in a user input into a class, I've tried multiple things and could really use some assistance.
Code so Far:
#include <iostream>
#include <string>
using namespace std;
// Base class
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
// Derived class
class FamilyFued{
public:
points;
};
int main ()
{
GameShow TN;
TN.Name
return 0;
}
Classes are used to abstract the state and behavior of things.
State is in the form of class attributes.
The behaviour is in the form of class methods (functions).
This is a behaviour:
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
So you need to put it inside a function:
class GameShow{
public:
string Name;
GameShow(){
cout << "Enter name of contestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};
Based on what you have, you might want to wrap cout >> ...; cin <<...; part in a constructor:
class GameShow{
public:
string Name;
GameShow()
{
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};
Other than that, please refer to: The Definitive C++ Book Guide and List
You code missing too many basics, you need start with a good c++ book.
using namespace std; // is bad idea, mentioned in stack overflow thousands of times.
bad
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
Your cout and cin function should be used inside a constructor
class GameShow{
public:
string Name;
GameShow()
{
std::cout << "Enter name of constestant: ";
std::cin >> Name;
std::cout << "Welcome "
<< Name
<< "! Let's get ready to play the FEUD!!" << std::endl;
}
};
TN.Name // what this line doing in your main function ??
Assuming you wanted to print Name of GameShow class member, so change your line to below.
std::cout << TN.Name;
Hi I have a code that's look's like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, classes;
cout << "Enter your name:" << endl;
getline(cin, name);
cout << "Enter your class:" << endl;
getline(cin, classes);
cout << "your name is " << name << "and your class is in " << classes << endl;
cout << "\n\n";
system("pause");
return 0;
}
and while I compile it compiled successfully without any warning or error message but, when I try to run the scripts it's suddenly disappear. how can I solve this problem? please someone help me
I recently use g++ version 9.2.0
I have just started C++ after working with C for almost a year. I'm writing a program for a user to input info about a song. I read that I should use getline() to read strings with spaces. Here is my code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
typedef struct Song
{
char title[20];
char album[20];
char artist[20];
int year;
} song;
//store song info
Song Input;
char inputStr[20];
int inputYear;
cout << "Enter the name of a song: ";
getline(cin, inputStr);
strcpy(Input.title, inputStr);
cout << "Enter the album of your song: ";
getline(cin, inputStr);
strcpy(Input.album, inputStr);
cout << "Enter the artist of your song: ";
getline(cin, inputStr);
strcpy(Input.artist, inputStr);
cout << "Enter the year your song was released: ";
cin >> inputYear;
Input.year = inputYear;
//print
cout << "Song title: " << Input.title << endl;
cout << "From album: " << Input.album << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Released: " << Input.year << endl;
return 0;
}
My compiler1 throws 3 errors, one for each of the getline() calls, not recognizing getline() despite the fact I have #include <string>. I have looked up sample usage of the getline() function.
Thanks for any help.
1I have wondered if this problem might concern an issue with the standard of C++ that my compiler supports. I did a bit of research and I did not find anything that helped me learn which standard I am using. Here's some info:
I'm using Terminal on Mac.
After g++ version:
Configured with: --prefix=/Applications/Xcode.app/Cont.../usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
These lines seem to be the only ones of use here, but I could give more info. If someone has any idea which standard of C++ this is, whether it's C++11, or C++14, or otherwise, that would also be very helpful. Thanks again.
UPDATE:
started from scratch, tried to take as much of your advice as possible while still sticking to some of what I know. No errors and works just as I hoped. Thanks for all your help.
New code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
struct Song
{
string title;
string artist;
string album;
string year;
}song;
int main()
{
Song Input;
cout << "Song? ";
getline(cin, Input.title);
cout << "Artist? ";
getline(cin, Input.artist);
cout << "Album? ";
getline(cin, Input.album);
cout << "Year? ";
getline(cin, Input.year);
cout << "Song: " << Input.title << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Album: " << Input.album << endl;
cout << "Year: " << Input.year << endl;
return 0;
}
The version of getline you are using takes a std::string as a parameter, not an array of char. If you want to use an array of char (and you shouldn't), you need to use the member function version:
cin.getline( some_char_array, array_size );
I would switch from using char arrays to using string's everywhere. For example I would do your code like this:
#include <string>
#include <iostream>
struct Song{
std::string title;
std::string album;
std::string artist;
int year;
};
int main()
{
//store song info
Song Input;
int inputYear;
std::cout << "Enter the name of a song: ";
getline(std::cin, Input.title);
std::cout << "Enter the album of your song: ";
getline(std::cin, Input.album);
std::cout << "Enter the artist of your song: ";
getline(std::cin, Input.artist);
std::cout << "Enter the year your song was released: ";
std::cin >> Input.year;
//print
std::cout << "Song title: " << Input.title << '\n';
std::cout << "From album: " << Input.album << '\n';
std::cout << "Artist: " << Input.artist << '\n';
std::cout << "Released: " << Input.year << std::endl;
return 0;
}
My preference is to not use using namespace std; but there's nothing wrong with it. Notice that using strings directly I don't need to copy things. I can use getline to do all that for me. I also don't need to worry about overrunning the size of the char array because string does that for me as well.
I have been trying to test build this old 'text adventure' thing I found,
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////
string name;
string shipName;
int health;
int crewCount;
int armyTotal, activeArmy;
int casualtiesCount, woundedCount, healedCount;
// 'food' as in a whole meal (beverage, chewables, etc)
int foodCount;
////////////////////////////////////////////////////// INTRODUCTIONS ///////////////////////////////////////////////////////
cout << "What is thy name?\nName: ";
cin >> name;
cout << endl << "What will you name your ship?\nShip Name: ";
cin >> shipName;
cout << "\nSETTING: You are floating through space on giant space cruiser " << endl << "known as the " << shipName << ".\n You are on a random patrol sorti, just looking out for any trouble...";
cout << "Press ENTER to continue...";
cin.get();
cout << "\nFrom here on out, type corresponding number to which choice you want to make.\nPress ENTER to continue...";
cin.get();
//////////////////////////////////////////////////////// BEGINNING ////////////////////////////////////////////////////////
cout << endl << "Admiral " << name << ", we need you on flight deck.";
cout << "1: Go to flight deck.";
cout << "2: Go to kitchen.";
cout << "3: Go to MedBay.";
cout << "4: Do nothing.";
}
and I get an error for:
cin >> name;
where ">>" matches no operands.
I clearly remember this code working at some point I do believe. If I try and skip ahead, I get an error where it can't find an exe (and there is no option to Build Final)
Sorry for not being clear, but I haven't used C++ for a few years now, quite rusty on just about everything. Any sort of wisdom to shed?
You need to #include <string>. That's where the actual operator is defined. Chances are that in the past, <iostream> may have included it perchance, which it's allowed to but not required (or guaranteed) to.