C++ 将字符串插入地图时出现编译错误
C++ Getting compilation error while insert string into a map
我使用get()
和getline()
来读取文件,然后输入到我的map()
。然后从地图类插入到AVLTree
并AVLNode
类。我应该怎么做才能解决常量字符串错误以及问题到底在哪里?感谢您的所有时间和帮助。这是整个代码。https://github.com/RichardHung/AVL-BST-Tree.git
60 13 SequenceMap.cpp [错误] 将 'const std::basic_string' 作为 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&( [使用 _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator] 的 'this' 参数丢弃限定符 [-permissive]
[注意] void std::basic_string<_CharT, _Traits, _Alloc>::p ush_back(_CharT( [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
[注意] 参数 1 没有已知的从 'const std::basic_string' 到 'char' 的转换
template <typename Comparable_1, typename Comparable_2>
class AvlNode
{
Comparable_1 element1; // sequence
vector<Comparable_2> element2; // enzy
AvlNode *left;
AvlNode *right;
int height;
bool exist;
AvlNode( const Comparable_1 & theElement1, const Comparable_2 & theElement2, AvlNode *lt, AvlNode *rt, int h = 0, bool ex = false):
element1( theElement1 ),element2( theElement2 ), left( lt ), right( rt ), height( h ), exist( ex ) { }
friend class AvlTree<Comparable_1, Comparable_2>;
};
template <typename Comparable_1, typename Comparable_2>
void AvlTree<Comparable_1, Comparable_2>::insert( const Comparable_1 & x, const Comparable_2 y, AvlNode<Comparable_1, Comparable_2> * & t )
{
if( t == NULL )
t = new AvlNode<Comparable_1, Comparable_2>( x, y, NULL, NULL, true );
else if( x < t->element1 )
{
insert( x, y, t->left );
if( height( t->left ) - height( t->right ) == 2 )
if( x < t->left->element1 )
rotateWithLeftChild( t );
else
doubleWithLeftChild( t );
}
else if( x > t->element1 )
{
insert( x, y, t->right );
if( height( t->right ) - height( t->left ) == 2 )
if( x < t->right->element1 )
rotateWithRightChild( t );
else
doubleWithRightChild( t );
}
else // ( x == t->element1 )
{
t->element2 = y ;
}
t->height = max( height( t->left ), height( t->right ) ) + 1;
}
template <typename Comparable_1, typename Comparable_2>
class SequenceMap
{
public:
Comparable_1 key;
Comparable_2 value;
AvlTree < Comparable_1, Comparable_2 > avl;
BinarySearchTree < Comparable_1, Comparable_2 > bst;
SequenceMap(){};
~SequenceMap();
}
template <typename Comparable_1, typename Comparable_2>
void SequenceMap<Comparable_1, Comparable_2>::AVLinsert( const Comparable_1 & str, const Comparable_2 & set )
{
if ( avl.isEmpty( ) )
{
key = str;
value = set;
avl.insert( key, value );
}
else // check the enzy is duplicated or not
{
for( int i=0; i<set.size( ); i++ )
{
if ( avl.findEnzyme( key, set ) ) // if cannot find the enzy in the node
{
merge( set );
avl.insert( key, value );
}
}
// if find the same enzy in the node, do nothing to avoid duplicated.
}
}
主.cpp
while ( ! fin.eof( ) )
{
if ( fin.peek( ) == '/' )
{
fin.get(); // skip "/" and move ptr to next sequence
while ( fin.peek() != 'n' )
{
getline(fin, sequence, '/');
cout << "enzyme: " << enzyme << endl;
cout << "sequence: " << sequence << endl;
map.insert(tree, sequence, enzyme);
sequence.clear();
fin.get(); // skip "/" and move ptr to next sequence
if(fin.peek() == '/')
fin.get();
}
fin.get();
enzyme.clear();
}
else // if( enz != '/' )
{
enzyme += fin.get( );
}
}
您需要注意编译器警告。
在你的几个班级中,你有std::vector<Comparable_2>
成员,我想你的意思是只有普通的老Comparable_2
成员。如果不更改它,我就无法编译您的代码。据我所知,这是你的主要问题。不能只将 T
类型的对象分配给类型 vector<T>
的容器。根据编译器,它们不仅是不同的类型,没有隐式转换,而且这样的赋值在逻辑上没有意义。
您可能打算使用std::vector<Comparable_2>
.在这种情况下,您的语法在很多地方都是错误的;您将需要执行诸如element2.push_back(y)
而不是element2 = y
之类的操作。您还需要将比较(如element2 == y
(更改为类似std::find(element2.begin(), element2.end(), y) != element2.end()
,因为就像您无法为vector<T>
分配T
一样,您也无法将T
与vector<T>
进行比较。
但是,您有很多重要的编译器警告:不明确的if/else
块,许多函数并不总是返回值,其他函数从不返回值,等等。如果将element2
成员从 std::vectors
更改为 std::strings
仍然无法编译代码,我会考虑修复其中的一些警告 - 其中一些可能会导致您的应用程序执行您意想不到的事情。
这是指向 coliru 编译和运行应用程序的链接。我在此处将输入文件加载到另一个 coliru 实例中。
作为旁注,你真的不应该做using namespace std;
.
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?