0

我的 DynamicArray 几乎是一个 ArrayList。我想让用户拥有一个连续的内存块,其中元素一个接一个,并且如果需要还有额外的空间。我遇到的唯一问题是存储 ints/floats/bools 等...我决定将我的 _array 设置为 T** 以便我可以存储它们。

但是,我在将常量传递给 DynamicArray 的 add 方法时遇到了一些麻烦。在传递它们之前,我需要将值存储在变量中,否则会出现此错误:

    g++ main.cpp -Wall -Werror -std=c++0x

    main.cpp: In function ‘int main()’:
main.cpp:14:21: error: no matching function for call to ‘triforce::DynamicArray<std::basic_string<char> >::add(const char [5])’
main.cpp:14:21: note: candidates are:
DynamicArray.h:150:10: note: bool triforce::DynamicArray<T>::add(T&) [with T = std::basic_string<char>]
DynamicArray.h:150:10: note:   no known conversion for argument 1 from ‘const char [5]’ to ‘std::basic_string<char>&’
DynamicArray.h:176:10: note: void triforce::DynamicArray<T>::add(uint, T&) [with T = std::basic_string<char>, uint = unsigned int]
DynamicArray.h:176:10: note:   candidate expects 2 arguments, 1 provided

我的 DynamicArray 有一个名为 _array 的 T** 变量。

/**
144      * @brief Adds an element to the end of the array. The array will double in size,
145      * if needed.
146      * @param element The element to be added to the array.
147      * @returns Returns if the element was added successfully.
148      */
149     template<class T>
150     bool DynamicArray<T>::add(T& element)
151     {
152         if(_array == NULL)
153         {
154             errorMsg("Cannot add to null array");
155             return false;
156         }
157 
158         if(_size == _capacity)
159         {
160             increaseCapacity(_capacity * 2);
161         }
162 
163         _array[_size] = &element;
164         _size++;
165 
166         return true;
167     }

这有效:

13     string val1 = "val1";
 14     array.add(val1);

这不起作用

13     string val1 = "val1";
 14     array.add("val1");

我该如何做到这一点,以便该函数可以接受常量,例如仅传递 1 或字符串“hello”,而无需先将其放入变量中?我尝试将 const 放入参数中,但这似乎也不起作用。

4

2 回答 2

1

In the example below:

#include <type_traits>

template <class T>
void foo( T& arg )
{
  static_assert( std::is_same<T&, const char(&)[6]>::value, "Must be same" );
}

int main()
{
  foo( "Hallo world" );
  return 0;
}

the type is deduced as const char[6]. then becomes a reference to an array, and therefore has type:

const char (&array)[6]

The name of an array (without qualification by operator []) decays to type char*, or in this case const char*:

_array[_size] = &element;

therefore taking its address, the type of the rhs here above becomes const char*, and it is not possible to assign a variable of type const char* to a variable of type char& (which is returned by _array[size].

You could remedy this as follows:

template<class T>
void DynamicArray<T>::add( const T& element )
{
  if(_size == _capacity)
  {
    increaseCapacity(_capacity * 2);
  }
  _array[_size] = element;
  _size++;
}

//------ Added code --------
template<class T>
  template <int N> 
void DynamicArray<T>::add( const T (&array) [N] )
{
  for( std::size_t i = 0; i < N; ++i )
  {
    //Calls your add per element...
    add( array[i] );
  }
}

Note that you will not be able to use std::string in the code here above. For this another overload would be required. Also note that my functions have no return type (as yours did). The return type had no purpose in your code, as any errors (as result of e.g dynamic allocation) would have thrown.

Lastly, as mentioned elsewhere, the code here above are merely written to understand concepts. In reality one would use a std::vector, which is of course already a dynamic array.

于 2013-10-15T03:44:12.263 回答
0
于 2013-10-15T03:14:52.327 回答