我试图创建一个程序来读取一组节点,看看是否要创建一个具有重复名称的程序。
目前我收到错误C2228:' .compare'必须有class / struct / union
我认为getName()返回指向char数组的指针的事实可能与此有关,但我真的不知道该怎么做。
Node.h只包含Node的构造函数。
Main.cpp的
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "fstream"
#include "iostream"
#include "istream"
#include "new"
#include "Node.cpp"
int _tmain(int argc, _TCHAR* argv[])
{
//name of file to be opened
char filename[]="resource.txt";
//temporary holding place of strings
char temp[256];
//Node Array
Node<char [256]> * resourceArray;
resourceArray = new Node<char [256]>[1];
int resourceArraySize=1;
Node<char [256]> * tempNode;
std::ifstream reFile;
//File is opened
reFile.open(filename, std::ifstream::in);
if(reFile.is_open())
{
while(!reFile.eof()){
//get the first item on a line and store it in temp
if(reFile.getline(temp,256,' ')){
//
for(int i=0;i<=resourceArraySize;i++){
//if index is greater that array size, reallocate array
if(i>=resourceArraySize){
tempNode = new Node<char [256]>[2*resourceArraySize];
std::copy(resourceArray,resourceArray+resourceArraySize,tempNode);
delete resourceArray;
resourceArray = tempNode;
delete tempNode;
resourceArraySize = resourceArraySize * 2;
}
if(resourceArray[i].getName.compare(temp)){
}
}
}
if(reFile.getline(temp,256,'\n')){
//TODO:Create a node, if necessary and add to previous's nodes requires array.
}
}
//File is closed
reFile.close();
}else{
perror("Error opening file\n");
}
system("pause");
return 0;
}
node.cpp
#include "Node.h"
//Default Constructor
template<class T>
Node<T>::Node(){
}
//This constructor sets the next pointer of a node and the data contained in that node
template<class T>
Node<T>::Node(const T& item,Node<T>* ptrnext){
this->data = item;
this->next = ptrnext;
useable=true;
requiresSize=1;
}
//This method inserts a node after the current node
template<class T>
void Node<T>::insertAfter(Node<T> *p){
//Links the rest of list to the Node<T>* p
p->next = this->next;
//Links the previous node to this one
this-> next = p;
}
//This method deletes the current node from the list then returns it.
template<class T>
Node<T> * Node<T>::deleteAfter(){
Node<T>* temp = next;
if(next !=NULL){
next = next->next;
}
return temp;
}
template<class T>
Node<T> * Node<T>::getNode(const T& item, Node<T>* nextptr = NULL){
Node<T>* newnode; //Local pointer for new node
newNode = new Node<T>(item,nextptr);
if (newNode == NULL){
printf("Error Allocating Memory");
exit(1);
}
return newNode;
}
template<class T>
void Node<T>::setName(char input[]){
strncpy(name,input,sizeof(name));
}
template<class T>
char * Node<T>::getName(){
return name;
}
template<class T>
bool Node<T>::isUseable(){
return useable;
}
template<class T>
void Node<T>::setUseable(bool use){
useable = use;
}
template<class T>
void Node<T>::addRequirement(char item[256]){
int i=0,end=0;
char * temp;
while(end==0){
//if search index is larger than size of the array,reallocate the array
if(i>= requiresSize){
temp = new char [requiresSize*2][256];
std::copy(requires,requires + requiresSize,temp);
delete requires;
requires = temp;
delete temp;
requiresSize = requiresSize *2;
}
//if the index at requires is not empty, check to see if it is the same as given item
if(requires[i]!= NULL){
if(item.compare(requires[i])){
//if they are the same, break out of the loop, item is already included
break;
}else{
//otherwise, increase the index and check again (continue loop)
i++;
}
}else{
//if the index is empty, add the item to the list and break out of loop
requires[i]=item;
end=1;
break;
}
}
}
答案 0 :(得分:0)
getName
是一种方法。
将if(resourceArray[i].getName.compare(temp)){
更改为if(strcmp(resourceArray[i].getName(), temp) == 0){
答案 1 :(得分:0)
getName更准确地说是一个返回指针的方法,而resourceArray也是一个指针。尝试: 如果(resourceArray [Ⅰ] - &GT; getName-&GT;比较(TEMP))