我查看了所有其他类似主题的帖子,没有任何帮助,所以请不要标记为重复.
我正在定义main()一个const int SIZE = 20;.然后,我将此作为参数传递给我的函数Mode:
int* Mode(int* numbers, int & mode, const int SIZE)
{
int occurences[SIZE];
// Calcualte mode
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误expression must have a constant value.
我的函数调用(在main中)看起来像这样:
int* occurencesPtr = Mode(numbersPtr, mode, SIZE);
Run Code Online (Sandbox Code Playgroud)
有了SIZE开头的文字被定义20.
我理解错误是因为函数的版本SIZE仅在调用函数时获取其值(?),但我不知道如何解决这个问题.
我甚至尝试过传递给函数a const int * const SIZEPtr = &SIZE,但这也没有用.救命?
编辑:我不是想使用可变大小!! 请注意,我做了SIZE一个const无处不在!我只想使用相同的SIZE常量来声明我的数组.
编辑:动态数组不是我需要的.我只想要一个普通的,命名的数组,用传递给函数的常量大小值定义.
我已经使用C++已经有一段时间了,但有一个基本概念我无法理解.首先,我将列出两种将文本字符串分配给char*的方法.
方法1:
char * str = "Hello World";
Run Code Online (Sandbox Code Playgroud)
方法2:
char * str = new char [12];
strcpy(str,"Hello World");
Run Code Online (Sandbox Code Playgroud)
我对方法2非常熟悉.方法1让我头疼.我的问题是
我读过无数的C++教科书和文章.他们都告诉我方法1的工作没有详细说明反响.我自己的实验并没有产生令人信服的结果.
谢谢(也许请原谅我的英语不好)
编辑:实际上我在VS2015中使用带有tchar字符串的WinAPI编程,方法1编译完美.使用Unicode处理时,std :: string非常糟糕.
想象一下,在解决方案中有两个项目,一个是Unicode,另一个是多字节,这两个项目使用相同的库.在这个库里面,最好使用tchar.std字符串你必须明确告诉它是哪个版本.
我必须这样做,因为多字节项目是我需要注入另一个应用程序的DLL.DLL的unicode版本将使应用程序崩溃,只有多字节工作.
我正在教朋友C.我们正在使用结构和指针,我给了他一个程序来试用他的电脑.我们将逐行解构程序,这样他就能理解结构和指针是如何协同工作的.在我的结尾,我得到这个结果:
astr
中a的值是5 astr 中b的值是5.550000 astr
中c的值是77 astr
中d的值是888.888800
在他的计算机上,该程序主要工作,除了astr-> d的最后一个值,它打印出一些非常大的负数.所以我的问题是,为什么这会发生在他的电脑上,但我的工作正常?以下是违规代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
struct a_struct{
int a;
float b;
int c;
double d;
};
struct a_struct* astr;
astr = (struct a_struct*)malloc(sizeof(astr));
astr->a = 5;
astr->b = 5.55;
astr->c = 77;
astr->d = 888.8888;
printf("Value of a in astr is %d\n", astr->a);
printf("Value of b in astr is %f\n", astr->b);
printf("Value of c in astr is %d\n", astr->c);
printf("Value of d in astr is %lf\n", astr->d);
return …Run Code Online (Sandbox Code Playgroud) 我已经声明geta()函数返回对a的引用,我必须在int指针中收集它.代码工作正常,但为什么我不能使用注释行.是不是在做同样的事情?代码如下:
#include<iostream>
using namespace std;
class Foo
{
int a=6;
public:
int& geta()
{
return a;
}
};
int main()
{
Foo f;
int &z = f.geta(); //int *p = f.geta(); Why this doesn't work?
int *p = &z;
cout << *p;
}
Run Code Online (Sandbox Code Playgroud) 我曾经用新的方式在我的C++项目中分配内存
char* buffer = new char [size];
...
delete[] buffer;
Run Code Online (Sandbox Code Playgroud)
我真的很想继续前进并使用unique_ptr,就像这样
unique_ptr<char[]>buffer(new char[size]);
Run Code Online (Sandbox Code Playgroud)
但后来我用istream& get (char* s, streamsize n);它char*作为第一个参数,所以我该怎么办?我试过投射类型,但失败了.我也知道我可以使用vector<char>而不是指针,但我真的不想使用它.谢谢!
我在双打的向量转换成double数组中所看到这里:
不同的是,我想将指针a作为函数参数返回:
void getArray(double* a)
{
std::vector<double> v;
a = &v[0];
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,什么时候内存a被解除分配?或者我必须自己打电话delete[] a,这似乎很奇怪,因为我没有分配内存new.我已经阅读了上述答案的评论,但我仍不清楚.
有人能告诉我为什么我会收到Segmentation错误:11.代码来自Absolute Beginner's Guide to C,3rd Edition,Chapter 6ex1.
#include <stdio.h>
#include <string.h>
main()
{
char Kid1[12];
// Kid1 can hold an 11-character name
// Kid2 will be 7 characters (Maddie plus null 0)
char Kid2[] = "Maddie";
// Kid3 is also 7 characters, but specifically defined
char Kid3[7] = "Andrew";
// Hero1 will be 7 characters (adding null 0!)
char Hero1 = "Batman";
// Hero2 will have extra room just in case
char Hero2[34] = "Spiderman";
char Hero3[25];
Kid1[0] = 'K'; …Run Code Online (Sandbox Code Playgroud) 我正在尝试传递指针指针,以便在另一个函数中修改其内容.为了做到这一点,我已经读过我需要使用指针指针的指针,所以这里是我的"代码"到目前为止:
void modify(s8 ***strings){
// Modifies the strings
}
void main(){
s8 strings[9][95];
for(i = 0 ; i < 9 ; i++){
for(j = 0 ; i < 95 ; i++){
strings[i][j] = 0;
}
}
modify(&strings);
}
Run Code Online (Sandbox Code Playgroud)
s8是我正在使用的MicroBlaze使用的特殊类型,这是一个问题吗?我还读过我需要使用const,但我不明白在哪里以及为什么?
大小怎么样?我是否需要在内部使用malloc修改才能在内存中保留空间?或者可以将修改功能设置为:
void modify(s8 *strings[9][95])
Run Code Online (Sandbox Code Playgroud)
现在,我的问题如下:我在最后一行(修改(&strings))上收到有关错误指针类型的警告.
passing argument 1 of 'modify' from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答!
是否可以交换两个void指针的内容?我尝试通过解除引用两个指针来交换值:
void* a = (void*)50;
void* b = (void*)90;
*a = *b;
Run Code Online (Sandbox Code Playgroud)
但是,最后一行会抛出错误,因为无法取消引用void指针.
我认为可以通过首先将b的值转换为int然后将其强制转换为void*来实现:
void* a = (void*)50;
void* b = (void*)90;
int value = (int)b;
a = (void*)value; //a now equals b
Run Code Online (Sandbox Code Playgroud)
不幸的是,这需要我们知道a和b是什么类型的"假设"(我不知道这样的描述是否正确),在这种情况下,它们是整数.是否可以在不知道其"原始"类型的情况下交换两个void指针的内容?
谢谢.
在使用C++工作时遇到了麻烦.我已经在SO和其他地方尝试了几个答案(比如:从函数中返回一个抽象类,如何使抽象类正确返回另一个抽象类的具体实例?),但我仍然一直有麻烦 - 这些似乎并不完全适合......
我有一个抽象类,派生类:
class AbstractClass {
virtual std::string virtMethod() = 0;
}
class Derived : public AbstractClass {
std::string virtMethod();
}
Run Code Online (Sandbox Code Playgroud)
另外,我是一个单独的类,我正在尝试获取抽象类的返回类型(当然,返回派生类的实例).
我尝试过使用指针和引用:
AbstractClass* methodFromOtherClass() {
if (somethingIsTrue) {
Derived d = Derived();
return &d;
}
SomeOtherDerived s = SomeOtherDerived();
return &s;
}
Run Code Online (Sandbox Code Playgroud)
在Xcode中,它给了我警告:
返回与本地变量"d"关联的堆栈内存的地址
我尝试创建一个静态Derived对象,然后无法在调用我的"methodFromOtherClass()"的方法中销毁它.
我也尝试过智能指针(虽然有人可能会指出我明显误用它们):
std::unique_ptr<AbstractClass> methodFromOtherClass() {
if (somethingIsTrue) {
Derived d = Derived();
return std::unique_ptr<AstractClass>(&d);
}
SomeOtherDerived s = SomeOtherDerived();
return std::unique_ptr<AstractClass>(&s);
}
Run Code Online (Sandbox Code Playgroud)
以上,也许不出所料,给了我一个段落错误.
我习惯于在Java中轻松地做到这一点...任何帮助将不胜感激.C++目前对我来说不是一种非常强大的语言,所以它可能是我忽略的非常基本的东西.