I'm new to google test and currently I'm writing a test for my OOP program, my OOP program is like this:
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
typedef unsigned int NUM;
class Employee
{
protected:
NUM MaSoThue;
private:
NUM Luong;
NUM CMND;
NUM a;
NUM b;
public:
Employee()
{
MaSoThue = 0;
Luong = 0;
CMND = 0;
}
Employee(NUM mst, NUM luong, NUM cmnd)
{
MaSoThue = mst;
Luong = luong;
CMND = cmnd;
}
//get
int getMaSoThue() const { return MaSoThue; }
int getLuong() const { return Luong; }
int getCMND() const {return CMND;}
//set
void setMaSoThue(NUM mst) {if (MaSoThue==0) MaSoThue = mst;}
void setLuong(NUM luong) {Luong = luong;}
void setCMND(NUM cmnd) {if (CMND==0) CMND = cmnd;}
};
int main()
{
// Objects
Employee PhucTri(111,222,333);
Employee MinhDang;
MinhDang.setMaSoThue(1234);
MinhDang.setLuong(2);
MinhDang.setCMND(8888);
//PhucTri
cout <<"MST cua Phuc Tri: "<< PhucTri.getMaSoThue()<<"\n";
cout << "Luong cua Phuc Tri: " << PhucTri.getLuong() << "\n";
cout << "CMND cua Phuc Tri: " << PhucTri.getCMND() << "\n\n";
//MinhDang
cout << "MST cua Minh Dang: " << MinhDang.getMaSoThue() << "\n";
cout << "Luong cua Minh Dang: " << MinhDang.getLuong() << "\n";
cout << "CMND cua Minh Dang: " << MinhDang.getCMND() << "\n";
}
I created a new file, which is below:
#include <gtest/gtest.h>
#include "FileCode.cc"
int main(){}
TEST(No1, PhucTri){
EXPECT_EQ(PhucTri.getMaSoThue(),111);
}
The compiler says that the object "PhucTri" isn't declared in this scope, but I did create it in my first file, is there any way I can get it right on the object ?
In general, Try to not include .cpp files, declare your class and its methods inside a .h file, define methods in .cpp and then create a test file that includes your header.
You have two options here, either define a test class that has an instance of your class follows instructions here.
Or do something like this :
#include <gtest/gtest.h>
#include "FileCode.h"
TEST(No1, PhucTri)
{
Employee PhucTri(111,222,333);
// initialize your data
// ......
EXPECT_EQ(PhucTri.getMaSoThue(),111);
}
I am working on some code to make a wallet to hold different currencies and this is my first time programming in c++ as a c programmer. Every time I make a new currency I want to add it to the list of valid Currencies that my wallet will be able to hold. To do this I make a currency class with a list that I want to add to every time a new currency is spawned. The error I get is error: no matching function for call to ‘std::__cxx11::list<Currency>::push_back(Currency*) CurrencyList.push_back(this);"\
Currency.h looks like:
#ifndef CURRENCY_H
#define CURRENCY_H
#include <string>
#include <list>
class Currency {
public:
//Instances of class
int id;
float max;
float left_over;
std::string coinName;
//Methods
float buyFrom(float amount);
float sellBack(float amount);
//constructor
Currency();
};
extern std::list<Currency> CurrencyList; //global list
#endif
Currency.c looks like
#include "currency.h"
#include <iostream>
Currency::Currency() {
Currency::id = 0;
std::cout << "Input name :" << std::endl;
std::cin >> Currency::coinName;
std::cout << "Input max :" << std::endl;
std::cin >> Currency::max;
Currency::left_over = Currency::max - 0;
CurrencyList.push_back(this);
}
float Currency::buyFrom(float amount) {
Currency::left_over-=amount;
std::cout << "Currency just lost :" << amount << "remaining is : " << Currency::left_over << std::endl;
}
float Currency::sellBack(float amount) {
Currency::left_over -= amount;
std::cout << "Currency just gained : " << amount << " remaining is : " << Currency::left_over << std::endl;;
}
The main is quiet simple it is only meant to spawn an object to test, that looks something like this.
Main.cpp
#include <iostream>
#include "wallet.h"
#include "currency.h"
int main(){
std::cout << "Hello World" << std::endl;
Currency currencyTest;
currencyTest.buyFrom(200.3);
}
Note that this is a pointer, but your list holds actual objects, not pointers.
So just dereference the pointer and you should be fine:
CurrencyList.push_back(*this);
This program allows the user to enter expenses for each season and then displays the values and then the total cost at the bottom. It's giving me this error, however,
In file included from /usr/include/c++/4.8.3/array:35:0,
from main.cpp:2:
/usr/include/c++/4.8.3/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
How do I fix this?
#include <iostream>
#include <array>
#include <string>
// constant data
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Spring", "Summer", "Fall", "Winter"};
// function to modify array object
void fill(std::array<double, Seasons> * pa);
// function that uses array object without modifying it
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(std::array<double, Seasons> * pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(std::array<double, Seasons> da)
{
using namespace std;
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
Read the error message! You need to compile with -std=c++11 or -std=gnu++11.
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
So, I'm making a text game. It opens and works fine then will abruptly close with no apparent trigger and open a new instance of itself. After it does this once it does not do it again. If I'm doing other things stupidly I would also like to know this. Heads up that this is a lot of code because I do not know where the problem lies. I tried to cut out the non important bits though. Would also like to know how to read write files in another directory that isn't the root one the exe is in. I am using Code::Blocks to compile. If the header files are important I can include them.
//#libraries
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "windows.h"
#include "math.h"
#include "time.h"
#include "Dungeon.h"
using namespace std;
//functions
int roomLogic();
void stats();
void options();
void devConsole();
void tutorial();
//Choose Class functions
void chooseClass();
void choosePaladin();
void chooseWarlock();
void chooseRanger();
void chooseWarrior();
void chooseRogue();
//global variables
int textSpeed;
int turns = 0;
bool debug = false;
//Player Stats
//Name and Such
int kills = 0;
int killScore = 0;
int damageDelt = 0;
int damageTaken = 0;
int score = 0;
string name;
string Class;
string race;
//Items
int keys = 0;
int money = 10;
int moneySpent = 0;
//Combat
int att = 10;
int def = 10;
int speed = 10;
int sneak = 10;
int eva = 10;
int perc = 10;
int accuracy = 10;
int health = 100;
int maxHealth = 150;
int mp = 50;
int magicPower = 10;
int main()
{
cout << "WHOLE BUNCH OF INTRO TEXT THAT YOU DO NOT NEED TO READ BECAUSE
THERE IS A LOT" << endl;
Sleep(1000);
//Text Speed Definition
cout << "How do you like your text speed? 'Slow', or 'Fast'?" << endl;
cout << "Type your answer and press ENTER to send" << endl;
string textSpeedInput;
cin >> textSpeedInput;
bool success = false;
//a while statement to get them to say yes no and set a text speed, not
important
cout << "If you have never played the game before and would like a
tutorial, type \n'tutorial' if not type 'no'" << endl;
string tutor;
cin >> tutor;
bool tut = false;
//another while statement thing that I cut out the function literally just
couts
chooseClass();
stats();
roomLogic();
system("PAUSE");
exit(0);
return 0;
}
void chooseClass()
{
cout << "Choice is fun, but randomness can be too, Would you like to
choose your class and race(Yes) or get a random class and race(No)" <<
endl;
string willChoose;
cin >> willChoose;
if(willChoose == "Yes" || willChoose == "yes")
{
cout << "Would you like to be a:\n" << endl;
cout << "Paladin (+3 Defense, -1 Speed)," << endl;
cout << "Warlock (+3 Magic Power, -1 Defense)," << endl;
cout << "Ranger (+2 Accuracy, +1 Perception, +1 Speed, -2 Defense)," <<
endl;
cout << "Warrior (+2 Attack, +1 Defense, -1 Magic Power), or" << endl;
cout << "Rogue (+2 Stealth, +1 Attack, +1 Evasion, +1 Speed, -3
Defense)?" << endl;
bool hasChoosen = false;
cin >> willChoose;
//another cut out while statement
code has broken by this point. I have no clue why. Please help is it an augment I need in my int main or something?
Here is an example read write I use just so you know:
ifstream x_file (".filenamehere.txt"); x_file >> variable to change;
x_file.close();
And to write:
ofstream y_file (".filename.txt"); y_file << thing to write; y_file.close();