一些关于C++的问题
#include"D:\MyDocuments\VisualStudio2012\Projects\Project1\Project1\Sales_item.h"#inc...
#include "D:\My Documents\Visual Studio 2012\Projects\Project1\Project1\Sales_item.h"
#include <iostream>
struct item_statistic
{
Sales_item item;
struct item_statistic *left;
struct item_statistic *right;
};
int get_book(Sales_item *books);
struct item_statistic *addbook(Sales_item *book, struct item_statistic *p);
int main()
{
int c;
Sales_item *books;
item_statistic *root = NULL;
while (get_book(books) != EOF)
root = addbook(books, root);
return 0;
}
struct item_statistic *addbook(Sales_item *book, struct item_statistic *p)
{
int c;
if (p == NULL) {
p = (struct item_statistic *) malloc(sizeof(struct item_statistic));
p->item = *book;
p->left = NULL;
p->right = NULL;
}
else if (c = strcmp(*book->isbn, p->item.isbn) > 0)
p->right = addbook(book, p->right);
else if (c = strcmp(*book->isbn, p->item.isbn) < 0)
p->left = addbook(book, p->left);
else
p->item += *book;
return p;
}
int get_book(Sales_item *books)
{
Sales_item *p;
int c;
p = books;
std::cin >> *p->isbn >> c;
return c;
}
上面是程序,下面是错误信息(有多个,不过放不下了。。。):
1>d:\my documents\visual studio 2012\projects\project1\project1\源1.cpp(31): error C2276: “*”: 绑定成员函数表达式上的非法操作
求大神的解答,最好详细一些 展开
#include <iostream>
struct item_statistic
{
Sales_item item;
struct item_statistic *left;
struct item_statistic *right;
};
int get_book(Sales_item *books);
struct item_statistic *addbook(Sales_item *book, struct item_statistic *p);
int main()
{
int c;
Sales_item *books;
item_statistic *root = NULL;
while (get_book(books) != EOF)
root = addbook(books, root);
return 0;
}
struct item_statistic *addbook(Sales_item *book, struct item_statistic *p)
{
int c;
if (p == NULL) {
p = (struct item_statistic *) malloc(sizeof(struct item_statistic));
p->item = *book;
p->left = NULL;
p->right = NULL;
}
else if (c = strcmp(*book->isbn, p->item.isbn) > 0)
p->right = addbook(book, p->right);
else if (c = strcmp(*book->isbn, p->item.isbn) < 0)
p->left = addbook(book, p->left);
else
p->item += *book;
return p;
}
int get_book(Sales_item *books)
{
Sales_item *p;
int c;
p = books;
std::cin >> *p->isbn >> c;
return c;
}
上面是程序,下面是错误信息(有多个,不过放不下了。。。):
1>d:\my documents\visual studio 2012\projects\project1\project1\源1.cpp(31): error C2276: “*”: 绑定成员函数表达式上的非法操作
求大神的解答,最好详细一些 展开
2个回答
展开全部
->是指针成员选择运算符比解引用要高,所以应该是*(book->isbn),由于isbn不是个指针,所以解引用是非法的,可以去掉*,或者(*book).isbn(其中.是对象成员选择符)。另外,粗略一看,你的程序有不少问题,比如int get_book(Sales_item *books),books指针没有初始化,也没有new对象,然后就调用了->操作,肯定会出问题的。还有就是C++里面参数的传递有是拷贝传递,形参指针和实参指针实际上不是同一个指针,只是他们对象的对象是同一个而已。这里root = addbook(books, root);这个函数中因为有malloc(new)会分配空间并由p来指向,但因为你return了p并赋给了root,不然就会出问题,最好的办法还是把root的地址传过去。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询