问题是数组的声明。
我们可以评论
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <list>
template <typename Type, size_t const SIZE>
class dummy_array {
Type data[SIZE] = {};
public:
dummy_array(){}
~dummy_array(){}
Type& operator[](size_t const index)
{
if (index < SIZE)
return data[index];
throw std::out_of_range("index out of range");
}
Type const& operator[](size_t const index) const
{
if (index < SIZE)
return data[index];
throw std::out_of_range("index out of range");
}
};
int main()
{
{
dummy_array<int, 6> arr();
arr[0] = 1;
arr[1] = 2;
for (int i = 0; i < 6; i++)
std::cout << arr[i] << " " ;
std::cout << std::endl;
}
return 0;
}
有人可以解释为什么用“ dummy_array arr();”声明吗?导致失败如下。 构建日志:
main.cpp: In function 'int main()':
main.cpp:34:12: error: pointer to a function used in arithmetic [-Wpointer-arith]
arr[0] = 1;
^
main.cpp:34:16:错误:分配了只读位置'* arr'
arr[0] = 1;
^
main.cpp:35:12:错误:指向算术[-Wpointer-arith]中使用的函数的指针
arr[1] = 2;
^
main.cpp:35:16:错误:分配了只读位置'*(arr + 1)'
arr[1] = 2;
^
main.cpp:38:27:错误:指向算术[-Wpointer-arith]中使用的函数的指针
std::cout << arr[i] << " " ;
^
答案 0 :(得分:2)
// plugins/google.gtag.client.js with "mode": "client
export default ({ store, app: { head, router, context } }, inject) => {
// Remove any empty tracking codes
const codes = store.state.siteMeta.gaTrackingCodes.filter(Boolean)
// Add script tag to head
head.script.push({
src: `https://www.googletagmanager.com/gtag/js?id=${codes[0]}`,
async: true
})
console.log('added script')
// Include Google gtag code and inject it (so this.$gtag works in pages/components)
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
inject('gtag', gtag)
gtag('js', new Date())
// Add tracking codes from Vuex store
codes.forEach(code => {
gtag('config', code, {
send_page_view: false // necessary to avoid duplicated page track on first page load
})
console.log('installed code', code)
// After each router transition, log page event to Google for each code
router.afterEach(to => {
gtag('config', code, { page_path: to.fullPath })
console.log('afterEach', code)
})
})
}
这可以看作是函数声明或变量声明。
编译器始终喜欢使用函数声明而不是变量声明,因此实际上您声明了一个名为arr的函数,不带任何参数并返回dummy_array。改用它:
dummy_array<int, 6> arr();