Can anyone explain my error from code blocks C++ [duplicate] - c++

This question already has answers here:
Problems importing libraries to my c++ project, how to fix this?
(2 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm trying to create a classes in separate files using codeblocks. I get the error in function '_start' undefined reference to 'main'. I'm sure its a linkage problem but can't see where. In my program I’m trying to get a die, let the user decide how many sides the dice has, then roll the die a user specified amount of times.
die.h file///////////////////////////////////
#include <iostream>
#include <string>
#ifndef DIE_H
#define DIE_H
using namespace std;
class die{
public:
die();//function prototype
int numsides;//member
void setNumsides(int numsides_);//setter
int getNumsides();// getter for size of dice
int value;
void setValue(int value_, int numsides_);
int getValue();
int roll;
void setroll(int roll_);
int getroll();
};
#endif// DIE_H
die.cpp//////////////////////////////////////////
#include "die.h"
#include <iostream>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
die::die()
{
//ctor
}
void die::setroll(int roll_)
{
roll=roll_;
}
int die::getroll()//amount o rolls
{
cout << "enter the ammont of rolls you would like" << endl;
cin >> roll;//amount of rolls you want
return roll;
}
void die::setValue(int value_, int numsides_)
{
value=value_;
numsides=numsides_;
}
int die::getValue()//get value function
{
//int roll;
value = (rand() % numsides) + 1;//sets roll value
return value;
}
void die::setNumsides(int numsides_)
{
numsides=numsides_;
}
int die::getNumsides()//get num of sides
{
cout << "how big of a dice would you like to roll " << endl;
cin >> numsides;//use this to determine dice
if(numsides < 4){//if dice is less than 4
cout << "Error has to be bigger than " << numsides << endl;
numsides = 6;//change to six sided dice
}
return numsides;
}
exercise1.cpp my main class/////////////////////////////////////
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include "die.h"
using namespace std;
int main()
{
die mydice;//create an object dice
mydice.getNumsides();//gets sides of dice
mydice.getValue();//gets amount of rolls
mydice.getroll();//rolls the dice value times
return 0;
}

Related

Can't put class with main function?

Something I am working on is making a code that focuses on making a class that reverses an order of numbers. This will then get put into the main code that will eliminate any trailing zeroes. I can't seem to wrap my head around how classes work and where I am going wrong. Here is my code:
Numbers.h
#pragma once
#include <iostream>
class Numbers
{
public:
int digit
private:
void Numbers::reverse();
};
Numbers.cpp
#include "Numbers.h
#include <iostream>
using namespace std;
void Numbers::reverse(){
int n, reversedNumber = 0, remainder;
cout << "Enter the number you would like to manipulate! " << endl;
cin >> n;
while (n !=0)
{
remainder = n % 10;
reversedNumber = reversed Number * 10 + remainder;
n /= 10;
}
//return *this;
}
Main.cpp
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "Numbers.h"
using namespace std;
int main()
{
Numbers.reverse;
system("pause");
return 0;
}
I can't seem to make the reverse function in my Numbers.cpp work in the main function. I am new to C++ and am not sure where I am going wrong. Any help would be appreciated!
OK, there are a lot of mistakes or learning errors in your code. Your header file should look something like:
#pragma once
class Numbers
{
public:
Numbers();
~Numbers();
int Reverse(int input); // Function is 'public'.
};
Your CPP file will then be (parts taken from S.O. post here):
#include "Numbers.h"
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
}
// No need to store the value in 'digit' since this
// is just an algorithm which can return the result.
int Numbers::Reverse(int input)
{
int ret = 0;
while(input > 0)
{
ret = ret * 10 + (input % 10);
input = input / 10;
}
return ret; // Return the reversed number and let the user decide what to do.
}
Then you can use your class as follows:
#include "Numbers.h"
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number to reverse: ";
cin >> num;
Numbers numClass;
cout << "Reversed number is: " << numClass.Reverse(num) << endl;
return 0;
}

fillVector with objects C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I have a class Player and a class Team.
I want to create a vector of Players in constructor of class Team. I have written a method called fillVector witch creates all the players. I have added method fillVector into class Team witch is shown bellow. I do not think class Player is necessary.
When I compile my programm with codeblocks there is the following error:
Team.cpp|9|undefined reference to `Team::fillTeamVector(std::vector >&)'|
|error: ld returned 1 exit status|
This is the Team.cpp code :
#include "Team.h"
#include <vector>
#include <iostream>
#include "Player.h"
Team::Team()
{
vector<Player> team;
fillTeamVector(team);
}
void fillTeamVector(vector<Player>& team){
cout << "How many players are in the team? " <<endl;
string name;
int teamSize,x,y,num,target_line;
cin >> teamSize;
for (int i=0 ; i<=teamSize ; i++){
cout << "Give player's name" << endl;
cin >> name;
cout << "Give player's number" << endl;
cin >> num;
cout << "Give player's x position" << endl;
cin >> x;
cout << "Give player's y position" << endl;
cin >> y;
cout << "Give player's target line" << endl;
cin >> target_line;
Player newPlayer(name,num,x,y,target_line);
team.push_back(newPlayer);
}
}
This is the Team.h code :
#ifndef TEAM_H
#define TEAM_H
#include <iostream>
#include <vector>
#include "Player.h"
using namespace std;
class Team
{
public:
Team();
void fillTeamVector(vector<Player>&);
};
#endif // TEAM_H
This is the main.cpp code :
#include <iostream>
#include <stdio.h>
#include "Player.h"
#include "Team.h"
using namespace std;
int main()
{
Team team;
return 0;
}
You have defined "fillTeamVector" as a free function in team.cpp, while it's a member function in the .h.
So fillTeamVector method of Team does not exist, but is called, hence the error.
In team.cpp, replace this :
void fillTeamVector(vector& team)
by this :
void Team::fillTeamVector(vector& team)
You've declared the function:
void fillTeamVector(vector<Player>& team){ // ...
But you forgot the "class-scope", that because that function is a member-function so you have to add:
void Team::fillTeamVector(vector<Player>& team){ // ...
Exactly what you've done for the constructor (Team::Team()).
You wrote a function called fillTeamVector.
This is not the same thing as a class method called Team::fillTeamVector, which is called by the constructor.

C++ Error: No Member in Class?

I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}

Multiple Definitions in C++ (Visual Basic 2010)

I'm attempting to practice some coding in my free time (combining a number of different interests of mine to help keep myself engaged) and I've encountered a odd error that I can't find the answer to. I have 4 files that I'm working with, two header files, one class definition file and a main file. I'm fairly confident I'm not including the Dice.h file more then once (however that is where the error points to and I'm not sure anymore, hence this question). What have I bungled here to produce these errors?
The error codes
Error 3 error LNK1169: one or more multiply defined symbols found (file path trimmed)
Error 2 error LNK2005: "int __cdecl dice(int,int)" (?dice##YAHHH#Z) already defined in Creature.obj (file path trimmed)
The filepath: c:\Users\Username\documents\visual studio2010\Projects\RPGTest\RPGTest\RPGTest.(error 3 referenced a .exe file, error 2 referenced a .obj file).
The code itself:
Dice.h
#ifndef SET_DICE_H_
#define SET_DICE_H_
#include <iomanip>
#include <iostream>
#include <stdlib.h>
using namespace std;
int dice(int number, int sides){
int total=0, dice;
srand(time(NULL));
int results=0;
do {
dice = rand()%sides+1;
total+=dice;
number--;
} while (number > 0);
results = total;
return results;
}
#endif
Creature.h
#ifndef CREATURE_H_
#define CREATURE_H_
#include <iomanip>
#include <iostream>
#include "Dice.h"
using namespace std;
class Creature {
public:
Creature(int,int,int,int,int,int,int,int,int,int,int,int);
void set_hp();
void set_saves();
void set_ac();
void set_bab();
void set_name();
void update_hp(int);
void update_ac(int);
void update_fsave(int);
void update_rsave(int);
void update_wsave(int);
int get_ac();
int get_hp();
int get_fsave();
int get_rsave();
int get_wsave();
int get_bonus(int);
int get_bab();
string get_name();
private:
int strength, dexterity, constitution, intellegence, wisdom, charisma;
int bab, fbsave, rbsave, wbsave;
int hdnum, hdsize;
int hp, fsave, rsave, wsave, ac;
string name;
};
#endif
Creature.cpp
#include "Creature.h"
#include <math.h>
#include <iostream>
using namespace std;
Creature::Creature(int strength,int dexterity,int constitution,
int intellegence,int wisdom,int charisma,int bab,int fbsave,
int rbsave,int wbsave,int hdnum,int hdsize){
strength = strength;
dexterity = dexterity;
constitution = constitution;
intellegence = intellegence;
wisdom = wisdom;
charisma = charisma;
bab = bab;
fbsave = fbsave;
rbsave = rbsave;
wbsave = wbsave;
hdnum = hdnum;
hdsize = hdsize;
}
int Creature::get_bonus(int stat){
int bonus = floor((double(stat)-10)/2);
return bonus;
}
void Creature::set_ac(){
ac=10+get_bonus(dexterity);
}
void Creature::set_hp(){
hp = dice(hdnum,hdsize) + get_bonus(constitution)*hdnum;
}
void Creature::set_saves(){
fsave = fbsave + get_bonus(constitution);
rsave = rbsave + get_bonus(dexterity);
wsave = wbsave + get_bonus(wisdom);
}
void Creature::set_bab(){
bab = hdnum;
}
void Creature::set_name(){
cout << "Please enter a name for this creature: ";
cout << "\nSorry! I don't work yet!";
cout << "\nInstead all creatures are named Larry!\n";
name = "Larry!";
}
void Creature::update_hp(int input){
hp = hp + input;
}
void Creature::update_fsave(int input){
fsave = fsave+input;
}
void Creature::update_rsave(int input){
rsave = rsave+input;
}
void Creature::update_wsave(int input){
wsave = wsave+input;
}
void Creature::update_ac(int input){
ac = ac+input;
}
int Creature::get_ac(){
return ac;
}
int Creature::get_hp(){
return hp;
}
int Creature::get_fsave(){
return fsave;
}
int Creature::get_rsave(){
return rsave;
}
int Creature::get_wsave(){
return wsave;
}
int Creature::get_bab(){
return bab;
}
RPGTest.cpp
#include "Creature.h"
#include <math.h>
//#include "Dice.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int str = dice(3,6), dex = dice(3,6), con = dice(3,6), intel = dice(3,6), wis = dice(3,6), cha = dice(3,6);
int fbs = dice(1,6), rbs = dice(1,6), wbs = dice(1,6);
int hdn = dice(1,10), hds = 8, bab = dice(1,8);
cout << "Welcome to RPG Creature Tester v0.1\n";
cout << "This .exe file is meant to test the creature class functions and definitions.\n";
cout << "This will be done by randomly generating and displaying a creature.\n";
cout << "What you don't see right now is the random generation of a creature.\n";
cout << "Once it's finished, the \'statsheet\' will be shown.\n";
cout << "Cheers!\n\n";
Creature potato (str, dex, con, intel, wis, cha, bab, fbs, rbs, wbs, hdn, hds);
potato.set_ac();
potato.set_hp();
potato.set_name();
potato.set_saves();
cout << "OUTPUT BRICK YAY\n";
cout << "Str: " << str << endl;
cout << "HP: " << potato.get_hp() << " AC: " << potato.get_ac() << " Fort/Reflex/Will Save: " << potato.get_fsave() << "/" << potato.get_rsave() << "/" << potato.get_wsave();
return 0;
}
Since I'm mainly self-taught I'm happy for any other advice but my main issue is that I'm not sure why I'm getting the "multiple" definition error. I did some research into other questions with similar error messages but I didn't see anything that immediately jumped out at me as "the answer".
Thanks all!
C++ works by compiling single translation units and then linking them together.
This means that each source file gets compiled on its own. Since the #include directive basically inserts all the code included, in your situation you end up having multiple translation units which define
int dice(int number, int sides) {
...
}
Compilation goes through fine but, when linking, multiple definition of this function are found so this generates the error.
To solve this problem you have two ways:
declare int dice(int, int) in a header file but define (implement it) in a source file
keep the definition as it is but prepend static to it. This tells the compiler that each translation unit will get its own dice method. This solution, although tempting, leads to binary size increase since you will have multiple implementation of the same method

No matching function for call to Dice::Dice (class constructor)

I'm creating a dice game. I'm building the files but get the following error:
No matching function for call to Dice::Dice
main.cpp:
#include "Dice.h"
#include <iostream>
using namespace std;
int main (){
Dice d(1,6);
cout << d.getRoll() << endl;
return 0;
}
Dice.h:
#ifndef DICE_H
#define DICE_H
class Dice
{
public:
Dice();
void getRoll(int m, int n);
};
#endif
Dice.cpp:
#include "Dice.h"
#include <ctime>
#include <iostream>
using namespace std;
Dice::Dice()
{}
void Dice::getRoll(int m, int n) {
srand(time(0));
(rand() % n)+m;
}
I see several problems with the code. Here's are my fixes and tips:
Firstly, your construction and method call of Dice will not compile:
Dice d(1,6); // you give arguments to the constructor
cout << d.getRoll() << endl; // your method call has no arguments
But you defined:
Dice(); // constructor takes no arguments
void getRoll(int m, int n); // method takes arguments
Secondly, srand only needs to be done once, not every time you call roll – maybe in the main function:
srand( (unsigned)time( NULL ) );
This seeds the generator so that you should get different random numbers every time the program runs. Call it only once, before the first dice roll.
Thirdly, your getRoll function returns nothing, meaning you get no value back. And you should name your variables according to what idea they represent in reality or your specification:
int Dice::getRoll(int maxEyes) { // Still no good abstraction
(rand() % maxEyes) + 1;
}
A real dice does not change its maxEyes at runtime. Why not try some object orientation instead of a function library class. Think about a real dice object! Here's a dice abstraction to start with:
main.cpp
#include "Dice.h"
#include <iostream>
using namespace std;
int main()
{
Dice::randomize(); // Try commenting this out and run the program several times, check the result, then comment it back in
Dice diceWith6Sides(6);
cout << "The 6 sided dice rolls a " << diceWith6Sides.getRoll() << endl;
cout << "The 6 sided dice rolls a " << diceWith6Sides.getRoll() << endl;
cout << "The 6 sided dice rolls a " << diceWith6Sides.getRoll() << endl;
Dice diceWith20Sides(20);
cout << "The 20 sided dice rolls a " << diceWith20Sides.getRoll() << endl;
cout << "The 20 sided dice rolls a " << diceWith20Sides.getRoll() << endl;
cout << "The 20 sided dice rolls a " << diceWith20Sides.getRoll() << endl;
return 0;
}
Dice.h
#ifndef DICE_H
#define DICE_H
class Dice
{
public:
Dice(int sides);
int getRoll();
static void randomize(); // Call only once
private:
int sides;
};
#endif
Dice.cpp
#include "Dice.h"
#include <time.h>
#include <stdlib.h>
Dice::Dice(int sides) :
sides(sides)
{
}
int Dice::getRoll()
{
return ((rand() % sides) + 1);
}
void Dice::randomize()
{
srand((unsigned)time(NULL));
}
Hope that is a good starting point. Have a lot of fun!