1
Answer

error: a template declaration cannot appear at block scope template

Photo of Guest User

Guest User

2y
2.5k
1

Hi Team

I need help i am using c++ libraries and trying to compile my program somehow i am experience the below errors

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<K, V>::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<typename K, typename V>
 ^~~~~~~~
prog.cc:262:1: error: a function-definition is not allowed here before '{' token
 {



// code

#include <map>
#include <limits>
#include <ctime>

template<typename K, typename V>
class interval_map {
	std::map<K,V> 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<K>::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 < keyEnd ), this designates an empty interval,
    // and assign must do nothing.
   void assign(K const& keyBegin, K const& keyEnd, V const& val) {

	if (!(keyBegin < keyEnd))
		return;

	auto pointerToEnd = m_map.upper_bound(keyEnd);

	if (!(this->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<ThinkCellKey<unsigned int>, char> imap {'a'};
  interval_map<unsigned int, char> 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;
}
C++

Answers (1)

0
Photo of Rajkiran Swain
28 40.7k 3.4m 2y

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<typename K, typename V> class interval_map { // ... };