2017-04-24 86 views
-1

我嘗試使用Putty在Linux中編譯我的C++代碼。但是我得到了各種錯誤。我修復了其中的一些。我認爲這些錯誤是由於我的模板功能而來的。也可能有一些內存分配錯誤。誰能幫忙?Linux C++編譯錯誤

錯誤:

[[email protected] sevenbridges]$ gcc -o asd Source.cpp 
In file included from Source.cpp:5: 
dictionary.h:24:7: warning: no newline at end of file 
Source.cpp:194:2: warning: no newline at end of file 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h: In static member function ‘static void std::__fill<<anonymous> >::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<HashTable<Dictionary>::HashEntry*, std::vector<HashTable<Dictionary>::HashEntry, std::allocator<HashTable<Dictionary>::HashEntry> > >, _Tp = HashTable<Dictionary>::HashEntry, bool <anonymous> = false]’: 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:568: instantiated from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<HashTable<Dictionary>::HashEntry*, std::vector<HashTable<Dictionary>::HashEntry, std::allocator<HashTable<Dictionary>::HashEntry> > >, _Tp = HashTable<Dictionary>::HashEntry]’ 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:330: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_fill_insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = HashTable<Dictionary>::HashEntry, _Alloc = std::allocator<HashTable<Dictionary>::HashEntry>]’ 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:658: instantiated from ‘void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = HashTable<Dictionary>::HashEntry, _Alloc = std::allocator<HashTable<Dictionary>::HashEntry>]’ 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:426: instantiated from ‘void std::vector<_Tp, _Alloc>::resize(size_t, _Tp) [with _Tp = HashTable<Dictionary>::HashEntry, _Alloc = std::allocator<HashTable<Dictionary>::HashEntry>]’ 
HashTable.h:107: instantiated from ‘void HashTable<HashedObj>::rehash() [with HashedObj = Dictionary]’ 
HashTable.h:97: instantiated from ‘void HashTable<HashedObj>::insert(const HashedObj&, int) [with HashedObj = Dictionary]’ 
Source.cpp:135: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:529: error: no match for ‘operator=’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = HashTable<Dictionary>::HashEntry*, _Container = std::vector<HashTable<Dictionary>::HashEntry, std::allocator<HashTable<Dictionary>::HashEntry> >]() = __value’ 
HashTable.h:26: note: candidates are: HashTable<Dictionary>::HashEntry& HashTable<Dictionary>::HashEntry::operator=(HashTable<Dictionary>::HashEntry&) 

Dictionary.h

ifndef DICTIONARY_H_ 
#define DICTINARY_H_ 
#include <string> 
#include <climits> 


class Dictionary{ 
public: 
    Dictionary(): code1(UINT_MAX), code2(UINT_MAX){} 
    Dictionary(int c1, int c2): code1(c1), code2(c2){} 
    Dictionary(const Dictionary & rhs): code1(rhs.code1), code2(rhs.code2) {} 

    bool operator == (const Dictionary & rhs) const; 
    bool operator != (const Dictionary & rhs) const; 
    Dictionary& operator = (const Dictionary & rhs) { 
     code1 = rhs.code1; 
     code2 = rhs.code2; 
     return *this; 
    } 
    unsigned code1; 
    unsigned code2; 

}; 

unsigned hashing(const Dictionary & d, int size); 


#endif 

Dictionary.cpp

#include "dictionary.h" 



bool Dictionary::operator == (const Dictionary & rhs) const{ 
    return (code1==rhs.code1)&& (code2 == rhs.code2); 
} 

bool Dictionary::operator != (const Dictionary & rhs) const{ 
    return (code1!=rhs.code1)&&(code2!=rhs.code2); 
} 

unsigned hashing(const Dictionary & d, int size){ 
    return (d.code1 + d.code2) % size; 
} 

Hashtable.h

#include <vector> 
using namespace std; 

template <class HashedObj> 
class HashTable{ 
public: 
    HashTable(int size = 101): 
     array(nextPrime(size)), currentSize(0){} 

    HashTable(const HashTable & rhs): 
     array(rhs.array), currentSize(rhs.currentSize) { } 

    const HashedObj & find(const HashedObj & x) const; 
    void findMax(int & maxcount, HashedObj &maxObject); 

    void insert(const HashedObj & x , int amount); 
    void remove(const HashedObj & x); 
    void increaseCount(const HashedObj & x); 
    int nextPrime(int n); 
    bool isPrime(int n); 


    enum EntryType { ACTIVE, EMPTY, DELETED }; 

private: 
    struct HashEntry{ 
     HashedObj element; 
     EntryType info; 
     unsigned int count; 

     HashEntry(const HashedObj & e = HashedObj(), EntryType i = EMPTY, unsigned int c = 0) 
      : element(e), info(i),count(c) { } 
    }; 

    int findPos(const HashedObj & x) const; 
    vector<HashEntry> array; 
    int currentSize; 
    const HashedObj ITEM_NOT_FOUND; 

    bool isActive(int currentPos) const; 

    void rehash(); 
}; 




template <class HashedObj> 
int HashTable<HashedObj>::findPos(const HashedObj & x) const { 
    int collisionNum = 0; 
    int currentPos = hashing(x, array.size()); 

    while (array[currentPos].info != EMPTY && array[currentPos].element != x) 
     (++currentPos) %= array.size(); 

    return currentPos; 
} 



template <class HashedObj> 
bool HashTable<HashedObj>::isActive(int currentPos) const { 
    return array[currentPos].info == ACTIVE; 
} 

template <class HashedObj> 
void HashTable<HashedObj>::remove(const HashedObj & x) { 
    int currentPos = findPos(x); 
    if (isActive(currentPos)) 
     array[currentPos].info = DELETED; 
} 

template <class HashedObj> 
const HashedObj & HashTable<HashedObj>::find(const HashedObj & x) const { 
    int currentPos = findPos(x); 
    if (isActive(currentPos)) 
     return array[currentPos].element; 

    return ITEM_NOT_FOUND; 
} 

template <class HashedObj> 
void HashTable<HashedObj>::insert(const HashedObj & x,int amount) { 
    // Insert x as active 
    int currentPos = findPos(x); 
    if (isActive(currentPos)) { 
     for (int i = 0; i < amount; i++) { 
      array[currentPos].count++; 
     } 
     return; 
    } 
    HashEntry b(x, ACTIVE, amount); 
    array[currentPos] = b; 

    // enlarge the hash table if necessary 
    if (++currentSize >= array.size()/2) 
     rehash(); 
} 



template <class HashedObj> 
void HashTable<HashedObj>::rehash() { 
    vector<HashEntry> oldArray = array; 

    // Create new double-sized, empty table 
    array.resize(nextPrime(2 * oldArray.size())); 
    for (int j = 0; j < array.size(); j++) { 
     array[j].count = 0; 
     array[j].info = EMPTY; 
    } 

    // Copy table over 
    currentSize = 0; 
    for (int i = 0; i < oldArray.size(); i++) 
     if (oldArray[i].info == ACTIVE) { 
      insert(oldArray[i].element,oldArray[i].count); 
     } 
} 


template <class HashedObj> 
void HashTable<HashedObj>::findMax(int &maxcount, HashedObj &maxObject) { 

    for (int currentPos = 0; currentPos < currentSize; currentPos++) { 
     if (isActive(currentPos)) { 
      if (array[currentPos].count > maxcount) { 
       maxObject = array[currentPos].element; 
       maxcount = array[currentPos].count; 
      } 
     } 
    } 

} 

template <class HashedObj> 
int HashTable<HashedObj>::nextPrime(int n) { 
    if (n % 2 == 0) 
     n++; 

    for (; !isPrime(n); n += 2) 
     ; 

    return n; 
} 
template <class HashedObj> 
bool HashTable<HashedObj>::isPrime(int n) { 
    if (n == 2 || n == 3) 
     return true; 

    if (n == 1 || n % 2 == 0) 
     return false; 

    for (int i = 3; i * i <= n; i += 2) 
     if (n % i == 0) 
      return false; 

    return true; 
} 
+3

「使用膩子」?你可以說,你正在嘗試使用鍵盤進行編譯。膩子與編輯有什麼關係? – user463035818

回答

0

您必須用#ifndef替換ifndefDictionary.h

您也在define中寫過DICTINARY_H_而不是DICTIONARY_H_

+0

對不起,我不能編輯,因爲我沒有足夠的聲譽,忘了複製那部分。我也改變了標題,但與它無關。 – theycallmefm