我在课堂上学习C ++。他们让我们在Ubuntu的虚拟机上使用Geany。我想念IDE的功能,所以我尝试了几个:Netbeans,Eclipse和Code :: Blocks。
我为家庭作业编写的代码在VM上的Geany中编译和运行没有问题。当我尝试所有其他IDE时,编译器给我一个错误。我在同一个虚拟机上尝试了Windows 7和Ununtu上的Netbeans和Eclipse。在寻找答案时,this help topic说Geany为我做了很多幕后工作。所以我尝试删除我对头文件和源代码的各种#include
语句,并且解决了问题(开玩笑!)并且无法弄清楚到底发生了什么
ArrayBagTester2.cxx :(这是我在Geany中编译/运行的内容)
#include <iostream>
#include <string>
#include "ArrayBag.h"
using namespace std;
int main()
{
// a bunch of stuff
}
ArrayBag.h :(由教授提供,不做任何修改/需要)
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 4;
ItemType* itemArray; //array must be dynamic; so pointers are very yes
int numberOfItems;
int myCapacity; //added variable to track capacity
public:
ArrayBag(int capacity = DEFAULT_CAPACITY); // new constructor
ArrayBag(const ArrayBag& anotherBag); // copy constructor
~ArrayBag(); //destructor
int getCurrentSize() const;
int getCapacity() const;
void resize(int newCapacity); // resize
bool isEmpty() const;
bool isFull() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
ArrayBag& operator=(const ArrayBag& anotherBag);
};
#include "ArrayBag.cxx"
#endif
BagInterface.h(由教科书作者提供,注意版权。我已经瘫痪以保持其版权。)
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 1-1.
@file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include <vector>
using namespace std;
template<class ItemType>
class BagInterface
{
public:
virtual .... stuff here, all virtual functions ...
};
#endif
ArrayBag.cxx:肉和土豆以及我们应该(仅)编辑的内容,错误从第9行开始:
#include <vector>
#include <cstddef>
#include <algorithm>
#include "ArrayBag.h"
// Constructor; creates and initializes an empty Bag of "capacity" size
template <class ItemType>
********************//LINE 9:**************************
ArrayBag<ItemType>::ArrayBag(int capacity)
{
myCapacity = capacity;
itemArray = new ItemType[myCapacity];
numberOfItems = 0;
}
// Copy constructor; creates and initializes Bag from another Bag
template <class ItemType>
ArrayBag<ItemType>::ArrayBag(const ArrayBag& anotherBag)
{
numberOfItems = anotherBag.getCurrentSize();
myCapacity = anotherBag.getCapacity();
itemArray = new ItemType[myCapacity];
for (int i = 0; i < numberOfItems; ++i)
itemArray[i] = anotherBag.itemArray[i];
}
//destructor
template <class ItemType>
ArrayBag<ItemType>::~ArrayBag()
{
delete [] itemArray;
}
// Assignment operator
template <class ItemType>
ArrayBag<ItemType>& ArrayBag<ItemType>::operator=(const ArrayBag<ItemType>& anotherBag)
{
// see if we're trying to assign to ourself, stupid user
if (&anotherBag == this)
return *this;
clear();
//ArrayBag<ItemType>::~ArrayBag();
delete [] itemArray;
//ArrayBag<ItemType>::ArrayBag(anotherBag);
numberOfItems = anotherBag.getCurrentSize();
myCapacity = anotherBag.getCapacity();
itemArray = new ItemType[myCapacity];
for (int i = 0; i < numberOfItems; ++i)
itemArray[i] = anotherBag.itemArray[i];
return *this;
}
// Return the number of Items being stored in the Bag
template <class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return numberOfItems;
}
// Return the capacity of the bag (the maximum Items it can store)
template <class ItemType>
int ArrayBag<ItemType>::getCapacity( ) const
{
return myCapacity;
}
//Resizes the bag's capacity to newCapacity
//if the new size is larger, copy all bag contents
// we don't downsize a bag in HW2
template <class ItemType>
void ArrayBag<ItemType>::resize(int newCapacity)
{
ItemType* oldArray = itemArray;
itemArray = new ItemType[newCapacity];
for(int i = 0; i < myCapacity; ++i)
itemArray[i] = oldArray[i];
delete [] oldArray;
}
// Report whether the Bag is empty
// Return true if the Bag is empty (storing no Items);
// Return false if Items exist in the Bag
template <class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return (numberOfItems == 0);
}
// Report whether the Bag is full
// Return true if the Bag is filled to capacity
// Return false if there is still room
template <class ItemType>
bool ArrayBag<ItemType>::isFull() const
{
//This bag is resizeable, so it's never full
return false;
}
// Give the Bag a new Item to store
// If Bag is full, double capacity and add newItem
// Else, Bag must add this Item to its Item array and update its numberOfItems
// If Bag is full after this, return true; else return false
template <class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newItem)
{
if (numberOfItems == myCapacity)
{
resize(myCapacity * 2);
myCapacity = myCapacity * 2;
}
itemArray[numberOfItems++] = newItem;
//This bag is resizeable, so it's never full
return false;
}
// Make the Bag act like an empty Bag again
template <class ItemType>
void ArrayBag<ItemType>::clear()
{
numberOfItems = 0;
}
// Remove an Item from the bag
// If Item is not there, nothing changes and we return false
// Else, we fill in its spot in that Item array and count number of Items down
template <class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anItem)
{
for (int i = 0; i < numberOfItems; ++i)
{
if (itemArray[i] == anItem)
{
itemArray[i] = itemArray[--numberOfItems];
return true;
}
}
return false;
}
// Check if an Item is in the Bag
// Return true if it is in the Bag, and false if not
template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anItem) const
{
for (int i = 0; i < numberOfItems; ++i)
if (itemArray[i] == anItem) return true;
return false;
}
// Check how many times an Item is in the Bag
// return 0 if it's not there; otherwise,
// return the number of times it occurs
template <class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anItem) const
{
int frequency = 0;
for (int i = 0; i < numberOfItems; ++i)
if (anItem == itemArray[i]) ++frequency;
return frequency;
}
// Make an output vector of Items from the bag (for checking)
template <class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
for (int i = 0; i < numberOfItems; ++i)
bagContents.push_back(itemArray[i]);
return bagContents;
}
以下是为ArrayBag.cxx中的每个方法生成的错误:
C:\ Users ... \ HW2 \ ArrayBag.cxx | 9 |错误:重新定义 'ArrayBag :: ArrayBag(INT)' | C:\用户\ HW2 \ ArrayBag.cxx | 9 |错误: 先前在此声明的'ArrayBag :: ArrayBag(int)'
......为了清晰起见而截断...
我想了解:
答案 0 :(得分:1)
包含文件ArrayBag.cxx
(这是非常奇怪的方式,但无论如何)并不打算编译。只需编译ArrayBagTester2.cxx
即可。
顺便说一句:我同意先前的评论,即这件事是怪异的......
答案 1 :(得分:1)
您不应该将ArrayBag.cxx编译为单独的编译单元。见这里:
Why can templates only be implemented in the header file?
模板实现文件不是单独的编译单元,但是您的命令行建议它被编译为一个。