为什么这不起作用?
package main
import "fmt"
type name struct {
X string
}
func main() {
var a [3]name
a[0] = name{"Abbed"}
a[1] = name{"Ahmad"}
a[2] = name{"Ghassan"}
nameReader(a)
}
func nameReader(array []name) {
for i := 0; i < len(array); i++ {
fmt.Println(array[i].X)
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
.\structtest.go:15: cannot use a (type [3]name) as type []name in function argument
Run Code Online (Sandbox Code Playgroud) 我正在学习Go目前,我制作了这个简单粗暴的库存程序,只是为了修改结构和方法来理解它们是如何工作的.在驱动程序文件中,我尝试从Cashier类型的items map中调用方法from和item type.我的方法有指针接收器直接使用结构而不是复制.当我运行程序时,我收到此错误.\driver.go:11: cannot call pointer method on f[0]
.\driver.go:11: cannot take the address of f[0]
Inventory.go:
package inventory
type item struct{
itemName string
amount int
}
type Cashier struct{
items map[int]item
cash int
}
func (c *Cashier) Buy(itemNum int){
item, pass := c.items[itemNum]
if pass{
if item.amount == 1{
delete(c.items, itemNum)
} else{
item.amount--
c.items[itemNum] = item
}
c.cash++
}
}
func (c *Cashier) AddItem(name string, amount int){
if c.items == nil{
c.items = make(map[int]item)
}
temp := …Run Code Online (Sandbox Code Playgroud) 我试图找出带有对象向量的结构和带有对象指针向量的结构的大小差异。
我编写的代码表明,即使在理论上,两个结构的大小也是相同的,至少基于它们的内容,它们应该是不同的。
根据结构的内容查找结构的正确大小的正确方法是什么?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Song{
string Name;
string Artist;
};
struct Folder{
vector<Song*> list;
};
struct Library{
vector<Song> songs;
};
int main(int argc, const char * argv[]) {
Library library;
Folder favorites;
Folder recentPurchaces;
library.songs.push_back(Song{"Human After All", "Daft Punk"});
library.songs.push_back(Song{"All of my love", "Led Zepplin"});
favorites.list.push_back(&library.songs[0]);
favorites.list.push_back(&library.songs[2]);
cout << "Size of library: " << sizeof(library) << endl;
cout << "Size of favorites: " << sizeof(favorites) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我一直在
错误LNK2019:函数_main中引用的未解析的外部符号"public:void __thiscall DynamicArray :: put(double)"(?put @?$ DynamicArray @ N @@ QAEXN @ Z).
我收到此错误的唯一一次是从DynamicArray类调用"put"函数
DynamicArray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <new>
#include <cstdlib>
using namespace std;
template <class T>
class DynamicArray
{
private:
T *origin;
T *allocator;
int size;
int current;
public:
DynamicArray();
DynamicArray(int);
DynamicArray(const DynamicArray&);
~DynamicArray();
void add(int);
void erase(int);
void empty();
void put(T);
void remove();
int getSize();
T &operator[](const int&);
const T operator=(const DynamicArray&);
void put(T&);
};
template <class T>
DynamicArray<T>::DynamicArray()
{
origin = new …Run Code Online (Sandbox Code Playgroud)