Linux C++编译错误

Linux C++ Compiling Error

本文关键字:错误 编译 C++ Linux      更新时间:2023-10-16

我尝试使用Putty在Linux中编译我的c ++代码。但是我遇到了各种错误。我修复了其中的一些。我认为这些错误是由于我的模板函数而出现的。可能也有一些内存分配错误。谁能帮忙?

错误:

[fmert@flow 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&)

词典.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

字典.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;
}

哈希表.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;
}

您必须在Dictionary.h中将ifndef替换为#ifndef

你还写了DICTINARY_H_而不是DICTIONARY_H_在你的define.