Notifications
Clear all

What is Error" n does not name a type" ?

13 Posts
3 Users
2 Likes
915 Views
barrie
(@barrie)
Member
Joined: 1 year ago
Posts: 86
Topic starter  

What is wrong with this?

Screenshot 2022 09 19 at 12.21.29 pm

   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@barrie

That line is not within the scope of a function, so the compiler is saying that t has no idea what n is. You'll need to move the assignment within one of the methods like setup() { ... } or loop() {...} etc. Then the compiler will recognize n as the int as defined.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@codeslinger)
Member
Joined: 4 years ago
Posts: 30
 

To expand on what @barrie said, the declaration of n, if you intend it to be global, should be before setup.  As the code currently exists, steup cannot reference n since it has not been declared.  C/C++ creates definitions and allocates variables in the order in which they are encountered.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@codeslinger 

 I don't think that's the problem here. This example places the definition before setup() as you suggest and the compiler still pukes on the assignment. 

 

 

int n=1;

void setup() {
    // put your setup code here, to run once:
}

    n=2;

void loop() {
    // put your main code here, to run repeatedly:

}

 

 

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 1 year ago
Posts: 86
Topic starter  

@will 

The attachment was to sort of replicate the problem in my sketch. I declared int n before "setup" in my sketch because I thought that was the birth place of global variables. I tried declaring it in setup but still got the error. @codeslinger are you saying that my declaration above setup should be OK? If so I'll dig deeper and post my sketch if I can't figure it out.

This post was modified 1 year ago by barrie

   
ReplyQuote
barrie
(@barrie)
Member
Joined: 1 year ago
Posts: 86
Topic starter  

@will are you suggesting I am not doing something silly? I hope so! 🤔 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@barrie 

This works ... 

int n=1;

void setup() {
    // put your setup code here, to run once:
    n = 2;
}

void loop() {
    // put your main code here, to run repeatedly:

}

 

And note that it's very unlikely that we can help you find a solution if you don't give us an accurate picture of what you're doing.

 

Anything seems possible when you don't know what you're talking about.


   
ron bentley reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 1 year ago
Posts: 86
Topic starter  

@will Thanks again. That solved the problem. 😀 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@barrie 

Good, now back to the salt mines !

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 1 year ago
Posts: 86
Topic starter  

@will By the way your avatar is remarkably like a character in a small animated video I made a long time ago. A funny spoof about advertising. I would show it here but it wouldn't be appropriate.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@barrie 

I chose this one because I always wanted to use something very like it in an advertisement with a slogan like "You'll be stunned by our customer satisfaction level" or something of that ilk.

Also, I have come very late to electronics and so this avatar most closely matches my level of capability and my approach to problem solving 🙂

Anything seems possible when you don't know what you're talking about.


   
ron bentley reacted
ReplyQuote
(@codeslinger)
Member
Joined: 4 years ago
Posts: 30
 

@will I screwed up and referenced the wrong person as the responder before.  i should have said that I was expanding on your reply.  I was trying to point out that, in addition to doing as you suggested and moving the assignment into a function, that the declaration should of n needs to be made before any functions referencing it. The source of the confusion that @barrie encountered is that the compiler can be rather obtuse with some error messages.  This is just one such example.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@codeslinger 

Not quite. In @barrie's original post, the definition of n DOES precede the use of n, so that is not the problem. The problem is the assignment occurs outside of the scope of any function of subroutine.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote