Hi Team
I need help i am using c++ libraries and trying to compile my program somehow i am experience the below errors
C++
In file included from prog.cc:208:0:
/opt/wandbox/gcc-7.2.0/include/c++/7.2.0/ctime: In member function 'void unit_test::interval_map::assign(const K&, const K&, const V&)':
/opt/wandbox/gcc-7.2.0/include/c++/7.2.0/ctime:58:1: error: 'namespace' definition is not allowed here
namespace std
^~~~~~~~~
prog.cc:210:1: error: a template declaration cannot appear at block scope
template
^~~~~~~~
prog.cc:262:1: error: a function-definition is not allowed here before '{' token
{
// code
#include
#include
#include
template
class interval_map {
std::map m_map;
public:
// constructor associates whole range of K with val by inserting (K_min, val)
// into the map
interval_map( V const& val) {
m_map.insert(m_map.end(),std::make_pair(std::numeric_limits::lowest(),val));
}
// Assign value val to interval [keyBegin, keyEnd).
// Overwrite previous values in this interval.
// Conforming to the C++ Standard Library conventions, the interval
// includes keyBegin, but excludes keyEnd.
// If !( keyBegin operator[](keyEnd) == val))
{
m_map.insert(std::make_pair(keyEnd, this->operator[](keyEnd)));
pointerToEnd = m_map.find(keyEnd);
}
auto pointerToBegin = m_map.lower_bound(keyBegin);
K keyToInsert = keyBegin;
if (pointerToBegin != m_map.begin() && std::prev(pointerToBegin)->second == val)
{
pointerToBegin = std::prev(pointerToBegin);
keyToInsert = pointerToBegin->first;
}
if(pointerToBegin != m_map.end())
m_map.erase(pointerToBegin, pointerToEnd);
m_map.insert_or_assign(keyToInsert, val);
}
// look-up of the value associated with key
V const& operator[]( K const& key ) const {
return ( --m_map.upper_bound(key) )->second;
}
};
int main(int argc, char* argv[])
{
// TODO: test interval map with different stl algorithm methods
// TODO: make 4 spaces tab
// interval_map, char> imap {'a'};
interval_map imap {'A'};
// imap.assign(3, 5, 'B');
// imap.assign(5, 7, 'C');
// imap.assign(2, 7, 'D');
// imap.assign(8, 10, 'k');
imap.assign(8, 12, 'k');
imap.assign(2, 12, 'k');
imap.assign(2, 12, 'b');
imap.assign(5, 12, 'b');
imap.assign(4, 10, 'b');
imap.assign(4, 12, 'b');
imap.assign(8, 13, 'a');
imap.assign(6, 9, 'j');
// imap.assign(4, 4, 'j'); // its ok
// imap.assign(0, 10, 'e');
// imap.assign(0, 10, 'e');
// imap.assign(2,6, 'B');
// imap.assign(3,10, 'C');
// imap.assign(4, 7, 'B');
// imap.assign(3, 5, 'B');
imap.show();
return 0;
}
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 23, 2023, 12:10 PM EDT
To fix this error, you should move the template declaration outside the class definition and place it before the class definition. In other words, replace the template declaration in line 210 with the following:
template class interval_map { // ... };