3个数据结构实验各有一个错误。
logBook,ListByArray的错误一样:LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/ListCreatedByArray.exe : fatal error LNK1120: 1 unresolved externals
class logBook
{
public:
//Constructor
logBook(int month,int year);
//Logbook marking operations
void putEntry(int day,int value);
int getEntry(int day) const;
//General operations
int getMonth() const;
int getYear() const;
int getDaysInMonth() const; //一年十二个月的天数
private:
bool isLeapYear() const; //判断闰年
//Data members
int logMonth,logYear,day,entries[31];
};
#include "logBook.h "
#include <iostream>
using namespace std;
bool logBook::isLeapYear() const
{
if((logYear%400==0)||((logYear%100!=0)&&(logYear%4==0)))
return true;
else return false;
}
int logBook::getDaysInMonth() const
{
switch(logMonth)
{
case 1:return 31;
break;
case 2:if(isLeapYear())
return 29;
else return 28;
break;
case 3:return 31;
break;
case 4:return 30;
break;
case 5:return 31;
break;
case 6:return 30;
break;
case 7:return 31;
break;
case 8:return 31;
break;
case 9:return 30;
break;
case 10:return 31;
break;
case 11:return 30;
break;
case 12:return 31;
break;
default:return 0;
}
}
logBook::logBook(int month,int year)
{
logMonth=month;
logYear=year;
day=getDaysInMonth();
int i;
for(i=0;i <day-1;i++)
{
entries[i]=0;
}
}
void logBook::putEntry(int day,int value)
{
entries[day-1]=value;
}
int logBook::getEntry(int day) const
{
return entries[day-1];
}
int logBook::getMonth() const
{
return logMonth;
}
int logBook::getYear() const
{
return logYear;
}
---------------------------------------
typedef int ElementType;
const int CAPACITY=1024;
class List
{
public:
List();
/* Creator
preconditon:no
postconditon:create an empty list */
bool empty() const;
/*Check whether the list is empty
precondition:no
postcondition:if the list is empty,return 1;else return 0*/
void insert(ElementType item,int pos);
/*Insert an item into the list to a certain positon
precondition:There 's enough space in the list.
0 <=pos <=mysize.
postcondition:Item has been inserted into right position of the list.*/
void erase(int pos);
/*Delete the item of a certain position
precondition:The list is not empty.
0 <=pos <=mysize.
postconditon:The item of the positon is deleted.*/
void display() const ;
/*Display a list]
precondition:ostream out has been opened.
postconditon:The items in the list has been inserted into out.*/
private:
int mySize; //The index of the list
ElementType myArray[CAPACITY];//The array used to store the list
};
#include <iostream>
#include "ListByArray.h "
using namespace std;
List::List()
{
mySize=0;
}
bool List::empty() const
{
return mySize==0;
}
void List::display() const
{
for(int i=0;i <mySize;i++)
cout < <myArray[i] < < " ";
}
void List::insert(ElementType item,int pos)
{
if(mySize==CAPACITY)
{
cout < < "***There 's no space to insert an element. ";
exit(1);
}
if(pos <0||pos> mySize)
{
cout < < "***Illegal location to insert-- " < <pos < < ".List unchanged.***\n ";
return;
}
for(int i=mySize;i> pos;i--)
myArray[i]=myArray[i-1];
myArray[pos]=item;
mySize++;
}
void List::erase(int pos)
{
if(mySize==0)
{
cout < < "***List is empty.***\n ";
return;
}
if(pos <0||pos> =mySize)
{
cout < < "Illegal location to delete-- " < <pos < < ".List unchanged.***\n ";
return;
}
for(int i=pos;i <mySize;i++)
{
myArray[i]=myArray[i+1];
mySize--;
}
}
---------------------------------------
const int maxListSize=10;
class Point
{
public:
Point(float x0=0,float y0=0)
{
x=x0;y=y0;
}
float x,y;
};
class PointList
{
public:
PointList();
void append(Point newPoint);
void clear();
bool isEmpty() const;
bool isFull() const;
void gotoBeginning();
void gotoEnd();
bool gotoNext();
bool gotoPrior();
Point getCursor() const;
void printPoint(Point a);
void showStructure() const;
private:
int size,cursor;//cursor is the index of a point array
Point points[maxListSize];
};
#include <iostream>
#include "PointList.h "
using namespace std;
PointList::PointList()
{
Point points[maxListSize];
cursor=-1;
size=0;
}
void PointList::append(Point newPoint)
{
if(size <=maxListSize)
{
if(cursor==-1)
{
points[0]=newPoint;
cursor=0;
}
else {
points[cursor+1]=newPoint;
cursor++;
}
size++;
}else
cout < < "The list is too fullto add a new point. " < <endl;
}
void PointList::clear()
{
int i=0;
for(i=0;i <cursor+1;i++)
{
points[i].x=0;
points[i].y=0;
}
size=0;
cursor=-1;
}
bool PointList::isEmpty() const
{
return size==0;
}
bool PointList::isFull() const
{
return size==maxListSize;
}
void PointList::gotoBeginning()
{
cursor=0;
}
void PointList::gotoEnd()
{
cursor=size-1;
}
bool PointList::gotoNext()
{
if((cursor!=size-1)&&(size!=0))
{
cursor=cursor++;
return true;
}
else return false;
}
bool PointList::gotoPrior()
{
if((cursor!=0)&&(size!=0))
{
cursor--;
return true;
}else return false;
}
Point PointList::getCursor() const
{
return points[cursor];
}
void PointList::printPoint(Point a)
{
cout < < "( " < <a.x < < ", " < <a.y < < ") " < <endl;
}
void PointList::showStructure() const
{
if(size==0)
cout < < "Empty list! " < <endl;
else{
int i=0;
for(i=0;i <size;i++)
{
printPoint(points[i]);
}
}
}
点列表的error是D:\Program Files\Microsoft Visual Studio\MyProjects\点列表ADT\PointList.cpp(100) : error C2662: 'printPoint ' : cannot convert 'this ' pointer from 'const class PointList ' to 'class PointList & '
Conversion loses qualifiers
如果将showStructure的const去掉,还将出现上述两程序同样的错误。
同时问一下,showStructure函数有改变size吗?为什么不能设置为const.
谢谢好心人儿^O^
[解决办法]
没有写主函数——main。
const对象不能作为非const引用类型的实参。
[解决办法]
类当然不用写main函数
他的意思可能是以为这是你的全部代码了,呵呵
“const对象不能作为非const引用类型的实参。”
就是说非const引用类型的实参是可以修改的,但是你传一个不能修改的const对象过去,就很危险了,所以就直接不让你传了
[解决办法]
类是不能单独编译成可执行文件的,要编译成可执行文件,你必须有入口点函数,对于console程序,需要实现int main(int,char*[])