问题描述
||
好的,我想解释一下在代码出现问题之前我想做什么。我正在创建一个学生记录系统。我正在尝试将信息从文本文件拉入记录系统。我试图在堆上使用数组存储信息。
我最关心的是前几个错误,因为我认为这些错误会导致随后的错误。由于我浏览了许多论坛,而它们没有引导我朝正确的方向发展,对您的帮助将不胜感激。提前致谢!
码:
#include \"address.h\"
#include \"student.h\"
#include \"date.h\"
#include \"record.h\"
#include \"name.h\"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#define pArray
#define students
#define Request
using namespace :: std;
enum {sNUMBER=50};
//Function Prototypes
void tPrint(string*,student pArray);
void sPrint(string*,student pArray);
void alpha(string*,student pArray);
int generate();
//Function DeFinitions
void tPrint(string*,student pArray,alpha)
{
if (alpha != 0)
{
alpha(pArray);
}//end if
int i;
for (i=0; i<sNUMBER;i++)
{
pArray->tPrint();
pArray+=1;
}//end for
};//end tPrint
void sPrint(string*,student pArray)
{
if (alpha != 0)
{
alpha(pArray);
}//ennd if
int i;
for (i=0; i<sNUMBER;i++)
{
pArray->sPrint();
pArray+=1;
}//end for
};//end sPrint
void alpha(string*,student pArray)
{
int i;//counter
int j;//counter
for (i=0;i<sNUMBER-1;i++)
{
for (j=0;j<sNUMBER-1-i;j++)
{
//combine first and last names into full names
if (strcmp((pArray->GetsName().GetfName())+=\" \" +=(pArray->GetsName().GetlName()),((pArray+1)->GetsName().GetfName())+=\" \"+=((pArray+1)->GetsName().GetlName()))>0)
{
swap(pArray[j],pArray[j+1]);
}
}//end for
}//end for
};//end alpha
int generate ()
{
srand(sNUMBER);
ofstream data;
data.open (\"data.txt\");
if (data.is_open())
{
int i; // counter
int j; // counter
//Name info
string new fName[sNUMBER];
string new lName[sNUMBER];
//Address info
string new add1[sNUMBER];
string new add2[sNUMBER];
//variables to generate names
string new fName1[5] = {\"Alfred\",\"Robert\",\"Ryan\",\"Hank\",\"Richard\"};
string new fName2[5] = {\"Hannah\",\"Lacie\",\"Crystal\",\"Kayce\",\"Susan\"};
string new lName1[5] = {\"Smith\",\"Jones\",\"Allison\",\"Johnson\",\"Moore\"};
string new lName2[5] = {\"Howard\",\"Williams\",\"Pinkston\",\"Cooley\",\"Vernon\"};
string new fNames[2] = {fName1,fName2};
string new lNames[2] = {lName1,lName2};
// variables to generate addresses
string new adds1[5] = {\"159 faternity st.\",\"399 Dorm Blvd.\",\"222 faternity st.\",\"777 Dorm Blvd\",\"82 Canal street\"};
string new adds2[10] = {\"Apartment A\",\"Apartment B\",\"Apartment C\",\"Apartment D\",\"Apartment E\",\"Apartment F\",\"Apartment G\",\"Apartment H\",\"Apartment I\"};
for (i=0;i<sNUMBER;i++)
{
for (j=0; j<5; j++)
{
strcopy(fName[i],fNames[i>25][j]);
strcopy(lName[i],lNames[i>25][j + floor(i/5)-(i>25)*(floor(i/5)-6)- 4*((j+i/5)>5)]);
}
}
for (i=0;i<sNUMBER;i++)
{
for (j=0; j<10; j++)
{
strcopy(add1[i],adds1[j]);
strcopy(add2[i],adds2[j + floor(i/10)-(i>25)*(floor(i/10)-11)- 4*((j+i/10)>10)]);
}
}
//deallocate unneeded variables
delete [] fName1;
delete [] fName2;
delete [] lName1;
delete [] lName2;
delete [] fNames;
delete [] lNames;
for (i=0; i<sNUMBER;i++)
{
// name info
Data << fName[i] << \"\\n\"; // first name
Data << lName[i] << \"\\n\"; // last name
// address info
Data << add1[i] <\" \\n\"; // add 1
Data << add2[i] <\" \\n\"; // add 2
Data << \"Indianapolis \\n\"; // city
Data << \"IN \\n\"; // state
Data << \"46168 \\n\"; // zip
// birthdate
Data << rand()%28 + 1 << \" \\n\"; //day
Data << rand()%12 + 1 << \" \\n\";//month
Data << 2011 -(rand()%49 + 12)<< \"\\n\";//year
//graddate
Data << rand()%28 + 1 << \" \\n\";//day
Data << rand()%12 + 1 << \" \\n\"; // month
Data << 2011 + (rand()%6+2)<< \" \\n\"; // year
//performance info
Data << (rand()%16)/(float rand()%4)<<\" \\n\"; //gpa
Data << (rand()%300)<< \" \\n\"; //credits
}
//Deallocate rest of memory
delete [] fName;
delete [] lName;
delete [] add1;
delete [] add2;
data.close();
}
else
{
cout << \"Your file did not open. Sorry \\n\";
}
return 0;
}//end generate}
错误如下:
C:\\Users\\main.cpp||In function ‘void tPrint(std::string*,student)’:|
C:\\Users\\main.cpp|25|error: too few arguments to function ‘void alpha(std::string*,student)’|
C:\\Users\\main.cpp|32|error: at this point in file|
C:\\Users\\main.cpp|37|error: expected primary-expression before ‘->’ token|
解决方法
1)您代码中的三个#defines可能完全是错误的
#define pArray
#define students
#define Request
特别是\“ pArray \”,因为稍后将其用作参数名称。
2)您正在将函数与零进行比较。这没有意义,因为测试将始终成功。
if (alpha != 0)
{
alpha(pArray);
}//end if
编辑:等等,我看,有一个(错误声明的)参数“隐藏”全局名称“ alpha”。好吧,这显然也不可行,因为参数没有名称(\“ alpha \”实际上是类型)。
3)正如David Thornley在评论中所指出的那样,您尝试使用单个参数调用该函数,由于您使用两个参数声明了该函数,因此显然无法使用。 (实际上,您根本不使用任何参数来调用它,因为pArray #define将被“扩展”为空字符串)
4)您尝试用来声明变量的语法是完全错误的
string new fName[sNUMBER];
我什至不知道那是什么意思。
而且可能还有更多...
,您确实需要获得有关C ++的教程或入门文本。有太多错误,我不知道从哪里开始...文件开头的#define
是没有意义的(您打算做什么?真正的效果是预处理器将丢弃这些单词),则不应通过指针处理字符串(通过引用,如果可能的话,可以使用常量引用),而是用两个参数声明tPrint
函数,并用三个参数定义不同的tPrint
重载,最后一个是不正确的,因为alpha
是不是类型。第一个和第二个参数(在预处理程序替换后)未命名,因此无法使用...
在进入第一行实际代码之前的所有操作...
,错误是:函数void alpha(std::string*,student)
的参数太少。
当我们在代码中查找时,您键入:alpha(pArray);
。
但是功能签名是:void alpha(string*,student pArray);
因此,将字符串作为参数传递可以解决该错误,但是代码中还有很多其他错误的地方:
void tPrint(string *,student pArray,alpha)什么是alpha?我只看到一个空的定义
字符串new fName [sNUMBER];不会编译。我想你是说string* name = new fName[sNUMBER];
,fName是什么?
还有更多奇怪的事情...
也许C ++教程更适合您:请参见此处
,您的大问题是5英镑。实际上,您正在将标识符定义为空。哪里有14英镑,现在什么都没有了。在C ++中,以ѭ15开头的任何行都是预处理程序命令,这意味着它将对程序的文本进行操作。为了实际定义标识符,您只需要本质上描述它即可。
您正在使用“ 14”作为函数参数,这意味着没有出现在外部范围内。如果预处理器的行为类似于C ++的任何其他部分,那么它将是无害的,但是它只是执行文本替换。您可能想让编译器在您未能定义某些内容时告诉您。
另一个问题是“参数过多”。您将alpha
声明为带有两个参数,但只有在tPrint
中早期调用它时才传递一个。
在您解决问题之前,我不会经历其余的错误。删除ѭ5并亲自阅读其他错误之后。