C ++ компилируется и запускается в Geany, а не в Netbeans, Eclipse, Code :: Blocks. Почему?

0

Я изучаю C++ в классе. Они используют нас с Geany на виртуальной машине с Unbuntu. Я пропускаю полномочия IDE, поэтому я попробовал несколько: Netbeans, Eclipse и Code :: Blocks.

Код, который у меня есть для домашней работы, компилируется и запускается без проблем в Geany на виртуальной машине. Когда я попробовал все другие IDE, компилятор дал мне ошибку. Я попробовал Netbeans и Eclipse в Windows 7 и 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 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 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 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 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:\Users...\HW2\ArrayBag.cxx | 9 | error: 'ArrayBag :: ArrayBag (int)', ранее объявленный здесь

... Усеченный для ясности...

Я хотел бы понять:

  1. Почему Geany работает, когда другие нет? Что на самом деле означает make make magic?
  2. Есть ли (легкое) исправление для Netbeans (не то, что Netbeans вызывает проблему) в Windows, чтобы исправить это? (Я, кажется, предпочитаю Netbeans, никакой другой причины.) Или:
  3. Что является более традиционным или стандартным способом включения этих отношений и файлов?
  • 0
    Как вы компилируете эти файлы?
  • 1
    Вместо этого чудовищного кода, почему бы не простая программа «Hello World» для тестирования этих различных IDE?
Показать ещё 8 комментариев
Теги:
netbeans
geany

2 ответа

1
Лучший ответ

Файл ArrayBag.cxx включен (это очень странный способ сделать это, но что угодно) и не предназначен для компиляции. Просто скомпилируйте ArrayBagTester2.cxx и все должно быть в порядке.

Кстати: я согласен с предыдущим комментарием, что эта штука - это чудовище...

  • 0
    Я нашел вариант в Code :: Blocks, чтобы сказать, что файл ArrayBag.cxx не должен компилироваться. Он не скомпилировал файлы .h (автоматически установил это). Теперь работает нормально.
  • 0
    У вас есть ответ о том, как лучше включить или более стандартный способ включить их? Я только учусь тому, чему учит класс.
1

Вы не должны компилировать ArrayBag.cxx как отдельный блок компиляции. Глянь сюда:

Почему шаблоны могут быть реализованы только в файле заголовка?

Файлы реализации шаблона не являются отдельными единицами компиляции, но ваша командная строка предполагает, что она была скомпилирована как одна.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню