Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.3k

error: a template declaration cannot appear at block scope template

Apr 23 2023 12:31 AM

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;
}

Answers (1)