Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am really confused about pointers. Read some articles about them and thought i got them, but i seems i didn't. I want to create a Team "Team1" which i normally would create by Team Team1; I have the name Team1 already stored as a string from an reading input of a textfile and thought i could create this team by pointers.
#include <iostream>
#include <string>
#include <vector>
class Team
{
private:
std::string m_teamname;
};
int main()
{
std::string wort = "team1";
std::string* pointer;
pointer = &wort;
std::string wort2 = *pointer;
std::cout << wort2;
Team *pointer;
std::cin.get();
}
I got the error C2371: 'pointer' : redefinition; different basic type " which is quite selfexplaning but still i dont get why it does not work as *pointer shows to the adress where the string "team1" is stored. Is there a way to do it ?
This has nothing to do with pointers, per se. You are defining the same variable twice with different types (as the error says: "pointer: redefinition").
std::string* pointer;
...
Team *pointer;
You define a variable named pointer with type std::string*, but then you try and define another variable with the same name. You'll have to rename one of them to something else.
You'd run into the same problem with any type:
int x;
double x; // <- error: there's already an x
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I'm working on this function for one of my classes and my pass count works just fine, however, my fail count ALWAYS prints out 12. I've been reading my code top to bottom and just can't seem to find the problem.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string passAndFailCount(string grades){
int pass_count;
int fail_count;
istringstream myStream(grades);
string grade;
while(myStream>>grade){
int i_grade=stoi(grade);
if (i_grade>=55){
++pass_count;
}
else{
++fail_count;
}
}
cout<<"Pass: "<<pass_count<<endl<<"Fail: "<<fail_count<<endl;
}
int main()
{
string grades;
getline(cin, grades);
passAndFailCount(grades);
}
Your problem are uninitialized variables.
int pass_count = 0;
int fail_count = 0;
and you're set.
For an explanation. Non-global variables (which automatically get initialized to 'default' (0) as per the standard), automatic and dynamic variables (like the one you are using), assume the value of 'whatever was in memory at the time of allocating the variable'.
Memory is never empty, there's always 'something' written in there.
So the memory of your fail_count variable just 'happened to be' 12 during allocation, which is why you start with that value. This value can be anything within scope.
By explicitly assigning a variable, you 'initialize' the variable, putting it into a defined state (0) and your program should start working as expected.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
CODE1(input):
#include <cstdio>
struct College {
char name[256];
};
void print_name(College* college_ptr) {
printf("%s College\n", college_ptr->name);
}
int main() {
College best_colleges[] = { "Magdalen", "Nuffield", "Kellogg" };
print_name(best_colleges);
}
OUTPUT:
Magdalen College
I have read that "At the slightest provocation, an array will decay into a pointer.A decayed
array loses length information and converts to a pointer to the array’s first element."
For example:
int key_to_the_universe[]{ 3, 6, 9 };
int* key_ptr = key_to_the_universe; // Points to 3
QUESTION1 :
I have also learned how a class and array are initialized but i dont understand how an array in a class can be initialized. According to me array in a class should be initialized ( referring CODE1) this way :
College best_colleges;
best_colleges.name[]={"Magdalen", "Nuffield", "Kellogg"};
QUESTION2:
(Referring CODE1)I also dont understand that if best_colleges array gets converted to pointer college_ptr which points to first element of it i.e
College* college_ptr = &best_colleges[0];
then in function "print_name" , "college_ptr->name" should be equal to "&best_colleges[0]->name". why we need to write "college_ptr-> name" ?
QUESTION 3:
I dont understand if best_colleges is a class or an array according to how it has been initialized.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
So I want to initialize a variable with a value like this:
ADM_job_t job{42};
The structure of ADM_job_t is this one:
typedef struct adm_job* ADM_job_t;
And adm_job looks like this:
struct adm_job {
uint64_t id;
};
So my idea was to intialize my variable with an int, because is the "final" type, but I'm getting an error saying that I can not initialize an adm_job with an int. I can not change the structure that has been provided. How can I do it?
Thank you!
As mentioned in the comments you do actually have to create a adm_job object.
Here is one way
adm_job obj{42};
ADM_job_t job = &obj;
Even if you can't change the adm_job structure, you can probably still ignore ADM_job_t for object creation, and use smart pointers instead. I recommend starting with something like this:
#include <memory>
struct adm_job {
uint64_t id;
};
typedef struct adm_job* ADM_job_t;
void foo(ADM_job_t p) {
}
int main () {
//auto job = std::make_unique<adm_job>(42); // C++20
auto job = std::unique_ptr<adm_job>{new adm_job{42}}; // C++11
foo(job.get());
}
job's type is std::unique_ptr<adm_job>. As you can see, you can still pass the result of job.get() to any old C function that makes non-owning use of ADM_job_t.
You needn't worry about deleting the object like this.
Read about unique_ptr. There are some other types of smart pointer depending on who else is going to "own" the pointed two object during its lifetime.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why can't I assign my new_age(age) to another variable (in this case it will be new)?
I get the following errors:
main.cpp:21:9: error: expected type-specifier before ‘=’ token
21 | new = new_age(age)
| ^
This is my code:
#include <iostream>
using namespace std;
int new_age (int & age)
{
return (age + 100);
}
int main()
{
int age {};
new = new_age(age)
cout << "How old are you "
cin >> age
cout << "In 100 years you will be " << new:
return 0;
}
The word new is a keyword in C++. It has a special meaning and cannot be used as name of a variable.
For a full list of all keywords see here.
As an aside, even if new was allowed as an identifier, you have not declared it. To declare and initialize it directly with the value you need to add the variable's type in front of the identifier:
int not_new = new_age(age);
(Also note the semicolon at the end of the declaration which you forgot or mistyped in a lot of statements as well.)
It also seems that you'd want to call new_age(age) after taking input from the user.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm experimenting with classes and I'm wondering why I'm getting an error saying "calvin" is not defined in the scope. Thanks.
#include <iostream>
#include <string>
using namespace std;
class people
{
public:
string name;
int age;
};
int main()
{
people peeps[10];
peeps[1].name = calvin;
peeps[1].age = 21;
cout << peeps[1].name << peeps[1].age;
}
Without quotes, calvin is a variable (which is undefined). You should make it a literal (i.e. "calvin").
firstly i would suggest if you had made and age private class members and also create get name/age and set name/age member functions to protect your data and finally
peeps[1].name = calvin;
//calvin is an undefined variable
you should have used string literals for example
peeps[1].name = "calvin";