C++编译和运行在Geany中,而不是在Netbeans,Eclipse,Code::Blocks中。为什么?
C++ compiles & runs in Geany, not in Netbeans, Eclipse, Code::Blocks. Why?
我正在课堂学习C 。他们让我们在带有Ubuntu的虚拟机上使用Geany。我错过了IDE的力量,所以我尝试了几种:NetBeans,Eclipse和Code :: Blocks。
我为作业编译和运行的代码在VM上没有问题。当我尝试所有其他IDE时,编译器会给我一个错误。我在Windows 7上尝试了Netbeans和Eclipse,并在同一VM中尝试了Ununtu。在寻找答案时,这个帮助主题说,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:用户... hw2 arraybag.cxx | 9 |错误:重新定义 'arraybag :: arraybag(int)'| c: users ... hw2 arraybag.cxx | 9 |错误: 'arraybag :: arraybag(int)'先前在此处声明
...为了清晰而被截断...
我想了解:
- 当其他人不这样做时,为什么Geany可以工作?
- 在Windows上是否有(易于)修复(不是Netbeans正在引起问题)来解决此问题吗?(我似乎更喜欢Netbeans,没有其他原因。)或:
- 更传统传统或标准包括这些关系和文件?
包括文件 ArrayBag.cxx
(这是非常奇怪的方法,但无论如何),并且不应该被编译。只需编译ArrayBagTester2.cxx
,就可以了。
顺便说一句:我同意以前的评论,即这东西是一种怪兽...
您不应将arraybag.cxx作为单独的汇编单元编译。请参阅此处:
为什么仅在标题文件中实现模板?
模板实现文件是不是单独的编译单元,但是您的命令行建议它被编译为一个。
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio Code "undefined reference to `WinMain@16'"
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 修改 VS Code 中的默认C++代码段
- 无法在Ubuntu上将共享库与Eclipse链接
- Eclipse CDT:单个项目中有多个C++文件
- 如何配置Visual Studio Code以使用cygwin,cmake和gcc进行调试
- C++ 运行时链接器错误 (liblog4cpp.so.5) Eclipse
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- VS Code C++:不准确的系统包括路径错误(wchar.h,boost/lambda/lambda.hpp)
- C++新手,想知道如何使用VS code 2019播放音频文件
- Eclipse/Netbeans 中的处理模式行
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- 如何在使用 SDL2 的 Eclipse 中构建 c++ 程序
- 使用 Eclipse CDT 进行 Cplex 配置
- C++编译和运行在Geany中,而不是在Netbeans,Eclipse,Code::Blocks中。为什么?
- Eclipse / Code::Block 不运行应用程序
- 对于我应该在Visual Studio和Eclipse (Code Red)中使用什么单元测试框架有什么想法吗?