我正在尝试使用代码块在单独的文件中创建一个类。我在函数'_start'未定义引用'main'时得到错误。我确定它是一个链接问题,但无法看到。在我的程序中,我试图让一个骰子,让用户决定骰子有多少边,然后用户指定的次数滚动骰子。
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;
}