Videos
Hi CS50 gang!
I'm really enjoying the course, but my brain hurts already at week 2. I've noticed an error when compiling a programme in C when I try to declare a variable inside a FOR loop in C (example below).
I just move my variable declaration to above the FOR loop, but I have two questions about this:
-
Why does this happen? Is it because the loop tries to declare the variable each time it iterates?
-
Does it cause problems to declare the variable (int letter_count) above the loop, like I have done in the example below? Or is there a better convention for declaring variables?
int letter_count = 0;
//loop to break text string into chars
for (int i = 0, n = strlen(text); i < n; i++)
{
//check if char i is lower or upper and increment letter-count by 1 if true
if (isupper(text[i]) || islower(text[i]))
{
letter_count++;
}
printf("%c", text[i]);
}Thank you!
I am getting this error numerous times in one file which leads me to think that its a simple thing im over looking yet I cannot find a solution to the problem. Any help is greatly appreciated.
I have a post.cpp and post.h (post.h is causing the problem) and it is giving the same error plus others. Please see the list here;
https://ibb.co/VBFc581
I have another class User - all the errors are related to this class for some reason. I am not getting any errors highlighted in the code file in VS only when I run the compiler. I will include the first error here as I dont want to post loads of code to make it hard to solve.
The Post class first error is std::vector<User*> taggesUsers; - you can see it has User in it.
#include "pch.h"
class Post
{
std::string poster;
time_t dateTime = DkDateTime::getTimeNow();
std::string message;
std::list<std::string> tags;
std::vector<User*> taggesUsers;My pch.h
#include <iostream> #include <string> #include <vector> #include <list> #include "DkDateTime.h" #include "User.h" #include "Post.h"
and my user
#pragma once
#include"pch.h"
class User
{
//Class members
std::string email;
std::string password;
std::string userName;
time_t lastLogin = DkDateTime::getTimeNow();;
public:
//Constructor
User();
User(std::string email, std::string password, std::string userName);
~User();
//Get&Set
std::string const getEmail() { return email; }
std::string const getPassword() { return password; }
std::string const getUserName() { return userName; }
time_t getLastLogin() { return lastLogin; }
void setEmail(std::string e) { email = e; }
void setPassword(std::string p) { password = p; }
void setUserName(std::string un) { userName = un; }
void setLastLogin(time_t ll) { lastLogin = ll; }
//Validation
bool validateEmail(const std::string email);
bool validatePassword(const std::string email);
bool validateUserName(const std::string userName);
//Overloads
friend std::ostream& operator<<(std::ostream &out, const User &usr);
friend std::istream& operator>>(std::istream& in, User& usr);
};I hope that if you can solve the first error I can get the rest as it seems to be anywhere in the Post.h file I use my User class.
So I've just learned about inheritance in my C++ programming class, and for one of my personal projects I wanted to create a card game. I have an object, 'aDeck', which I want to be a vector of 'aCard' objects.
This is what my .h file for the aDeck class looks like.
#pragma once
#include <vector>
#include "aCard.h"
using namespace std;
class aDeck : public vector<aCard>
{
public:
aDeck(int type);
~aDeck();
void drawCard(aDeck drawHand);
void populateHand(aDeck popHand);
void populateDiscard(aDeck gameDeck);
void outputAll();
void outputTop();
};
And this is the .h file for aCard
#pragma once
#include "aDeck.h"
#include <iostream>
using namespace std;
class aCard
{
public:
aCard();
aCard(int color, int number);
~aCard();
void displayCard();
private:
int cardColor;
int cardNumber;
};
This, however, is giving me the error of: "error C2065: 'aCard' : undeclared identifier"
Google says this is because i'm not including the .h file for aCard, which I am. So what would cause something like this?
Two notes which are unrelated to your problem (u/z3r0shade already answered that): 1) Don't use using namespace std;, especially in a header, and 2) you should avoid inheriting from std::vector, it's not designed to be used as a base class. Make your deck class have a std::vector<Card> member.
Why does aCard.h include aDeck.h?
You created a cycle of include. You include aDeck.h which then includes aCard.h which then includes aDeck.h and still doesn't know what aCard is. Remove the include of aDeck.h from aCard.h
int total = 0;
for (int i= 0; word[i] != '\0'; i++)
;
{
char c = word[i];
if (isalpha(c))
total += POINTS[get_index(c)];
}
return total;
}
int get_index(char c)
{
return tolower(c) - 'a';When I compile the code I get undeclared identifier 'i'. But when declare it I get declaration shadows a variable in the global scope [-Werror,-Wshadow]. Please help
They most often come from forgetting to include the header file that contains the function declaration, for example, this program will give an 'undeclared identifier' error:
Missing header
Copyint main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
To fix it, we must include the header:
Copy#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
If you wrote the header and included it correctly, the header may contain the wrong include guard.
To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.
Misspelled variable
Another common source of beginner's error occur when you misspelled a variable:
Copyint main() {
int aComplicatedName;
AComplicatedName = 1; /* mind the uppercase A */
return 0;
}
Incorrect scope
For example, this code would give an error, because you need to use std::string:
Copy#include <string>
int main() {
std::string s1 = "Hello"; // Correct.
string s2 = "world"; // WRONG - would give error.
}
Use before declaration
Copyvoid f() { g(); }
void g() { }
g has not been declared before its first use. To fix it, either move the definition of g before f:
Copyvoid g() { }
void f() { g(); }
Or add a declaration of g before f:
Copyvoid g(); // declaration
void f() { g(); }
void g() { } // definition
stdafx.h not on top (VS-specific)
This is Visual Studio-specific. In VS, you need to add #include "stdafx.h" before any code. Code before it is ignored by the compiler, so if you have this:
Copy#include <iostream>
#include "stdafx.h"
The #include <iostream> would be ignored. You need to move it below:
Copy#include "stdafx.h"
#include <iostream>
Feel free to edit this answer.
Consider a similar situation in conversation. Imagine your friend says to you, "Bob is coming over for dinner," and you have no idea who Bob is. You're going to be confused, right? Your friend should have said, "I have a work colleague called Bob. Bob is coming over for dinner." Now Bob has been declared and you know who your friend is talking about.
The compiler emits an 'undeclared identifier' error when you have attempted to use some identifier (what would be the name of a function, variable, class, etc.) and the compiler has not seen a declaration for it. That is, the compiler has no idea what you are referring to because it hasn't seen it before.
If you get such an error in C or C++, it means that you haven't told the compiler about the thing you are trying to use. Declarations are often found in header files, so it likely means that you haven't included the appropriate header. Of course, it may be that you just haven't remembered to declare the entity at all.
Some compilers give more specific errors depending on the context. For example, attempting to compile X x; where the type X has not been declared with clang will tell you "unknown type name X". This is much more useful because you know it's trying to interpret X as a type. However, if you have int x = y;, where y is not yet declared, it will tell you "use of undeclared identifier y" because there is some ambiguity about what exactly y might represent.
Hello, I'm new to programming and while coding my example program I ran into an issue that I've been trying to figure out how to resolve. When print_Menu_Info(); executes and it tries to execute print_Customer_Table(&c); I encounter the error in the title.
sales.c:101:24: error: use of undeclared identifier 'c'
Even though the line is being declared in line 12.
struct customer c = empty_customer;
#include <stdio.h>
#include <stdlib.h>
#include "sales.h"
int main(void)
{
menu_update();
return 0;
}
void menu_update(void){
struct customer c = empty_customer;
float f = 0.00;
int i = 0;
while (1) {
cls();
print_Menu_Info();
switch(get_User_Input()){
case 1:
printf("Customer First Name: ");
scanf("%s",c.first_Name);
break;
case 2:
printf("Customer Last Name: ");
scanf("%s",c.last_Name);
break;
case 3:
printf("Customer Amount: ");
scanf("%f",&f);
c.amount = f;
break;
case 4:
printf("Customer Card Number: ");
scanf("%s",c.card_Number);
break;
case 5:
printf("Customer Card Expiration Month: ");
scanf("%i",&i);
c.card_Expiration_Month = i;
break;
case 6:
printf("Customer Card Expiration Year: ");
scanf("%i",&i);
c.card_Expiration_Year = i;
break;
case 7:
printf("Sales Person: ");
scanf("%s",c.sales_Person);
break;
case 8:
print_Customer_Table(&c);
break;
case 9:
c = empty_customer;
break;
case 10:
exit(0);
default:
printf("Incorrect action ID!\n");
break;
}
}
}
void cls(void){
printf("\033c");
}
int get_User_Input(){
int a = 0;
char line[32];
printf("Please input an action ID: ");
if (fgets(line, sizeof(line), stdin) != NULL) {
sscanf(line, "%i", &a);
cls();
}
return a;
}
void print_Customer_Table(struct customer *cp){
printf(
"1. Customer First Name: %s\n"
"2. Customer Last Name: %s\n"
"3. Customer Amount: $%.2f\n"
"4. Customer Card Number: %s\n"
"5. Customer Card Expiration Month: %i\n"
"6. Customer Card Expiration Year: %i\n"
"7. Sales Person: %s\n",
cp->first_Name,
cp->last_Name,
cp->amount,
cp->card_Number,
cp->card_Expiration_Month,
cp->card_Expiration_Year,
cp->sales_Person);
}
void print_Menu_Info(void){
print_Customer_Table(&c);
printf(
"If you would like to update a value type the value you would like to update(1-7)\n"
"If you would like to send this as a email type 8\n"
"If you would like to clear all customer data type 9\n"
"If you would like to exit this program type 10\n");
} and sales.h
#define NAME_LEN_MAX 128
#define CARD_NUM_LEN_MAX 16
struct customer {
char first_Name[NAME_LEN_MAX + 1];
char last_Name[NAME_LEN_MAX + 1];
float amount;
char card_Number[CARD_NUM_LEN_MAX + 1];
int card_Expiration_Month;
int card_Expiration_Year;
char sales_Person[NAME_LEN_MAX + 1];
};
const static struct customer empty_customer = {{""},{""},0.00,{""},0,0,{""}};
void print_Customer_Table(struct customer *cp);
void print_Menu_Info(void);
void cls(void);
int get_User_Input();
void menu_update(void);Have you tried to initialize the Student object without the parentheses?
Student newStudent;
This calls the default constructor. What you did with
Student newStudent();
is to declare a function which takes no parameters and returns a Student object.
Just as a note, my professor said adding
#include student.h
to mymain.cppwould fix my issue. OBVIOUSLY NOT because I included it in mystudent.cpp
Your main() needs to know what is the the type Student to be able to create its object.
So your professor is correct.
Each source file is compiled separately, So when the compiler compiles main.cpp it needs to see the definition of the type Student.It can only do so if you include the header student. which defines the type in main.cpp. Note that including the header in student.cpp has no bearing on the fact that the definition needs to be seen in main.cpp because both of them are compiled as separate files.