Okay, probably a dumb question to you guys but I can't figure it out.
So I'm taking a c++ basics course in class and so far I'm struggling/crying.
I can't show you guys my code because I'm not allowed/there are consequences if I'm caught but I could probably give a example.
I'm using xcode. So when I compile, I get two errors below (image provided).
I searched for similar questions, but those seem too complex compared to what I'm doing. In addition, the only includes I have are iostream and string.
I know the problem occurs when I add an if statement in my main function. I know this because when I delete it, everything compiles as expected. Yet when I add it again to the main function, these errors occur.
So my question is, based on what I know, is it proper to add an if statements whenever in the main function?
Below is an example. I wrote the functions below and called above.
#include <iostream>
#include <string>
using namespace std;
// example functions that I just made up to explain the structure of my actual code.
//Don't bother trying to understand it. It's just to explain that
//I wrote my functions at the
// bottom and called it at the top.
int getNumberofWins(param1, param2);
string getTheName(int player1);
int executeCycle(string p1_name, string p2_name);
void stateWinner(string winner_name);
int main {
playerOne = getTheName(1);
playerTwo = getTheName(2);
r1 = executeCycle(playerOne, playerTwo);
r2= executeCycle(playerOne, playerTwo);
totalWin1 = getNumberOfWins(1, r1, r2);
totalWin2 = getNumberOfWins(2, r1, r2);
cout << totalWin1;
//This is the where I get the errors. When I delete the if statement,
//Everything compiles. When I add it, an error occurs.
if (totalWin1 == 2){
stateWinner(playerOne);
}
return 0;
}
string getTheName(int player1){
string playerOne;
string playerTwo;
if(player_number == 1){ code code code
}
}
int getNumberofWins (int param1, int param2){
code code code
}
int executeCycle(string p1_name, string p2_name){
code code code
}
void stateWinner(string winner_name){
if(!winner_name.empty()){
code code code
}
I hope it's fine if the code above isn't accurate. I think the point is that once I add my if statement to the main function, the two errors show up.
actually...now that I look at it, they both seem like similar errors. I just don't know why they both appear...
Sorry if this is an obvious answer or if it isn't clear.
The "announceWinner" function is not defined anywhere, ie there's no
void announceWinner () {
// code
}
anywhere. Either you haven't written it yet, or the file that contains it is not being compiled & linked with the main program.
Related
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> big_vector = {5,12,4,6,7,8,9,9,31,1,1,5,76,78,8};
vector<int> a = sub(big_vector);
cout<<a.size();
return 0;
}
vector<int> sub(vector<int> big_vector){
return {big_vector.begin() + 7, big_vector.end() - 2};
}
I get this error and do not know why
main.cpp:18:21: error: ‘sub’ was not declared in this scope
debugging and running different test
In C++ all objects must be declared before they are used.
vector<int> a = sub(big_vector);
Your reliable compiler reads this, and it has absolutely no clue, whatsoever, what this mysterious sub function is all about.
Human eyeballs that are owned by a carbon-based life form can easily see this function, later in the file. But your compiler is a beast of logic. It hasn't read that far ahead, into the source file. That's why, in C++, in this situation the function must be declared:
vector<int> sub(vector<int>);
Put this before the main function. The compiler reads this first, and learns all about this amazing function called sub(), which has one vector<int> parameter, and returns a vector<int> itself. So when it reads the contents of main(), which calls this function, it knows exactly what it's all about, and can proceed further.
Or, you can simply put the entire sub() function before the main() function. That'll work, too.
I've been attempting to figure out this bug for about an hour now. It's probably a really obvious syntax thing I'm overlooking. This is my first C++ project, and I don't have a good handle on the structure of the language.
Here's my header file:
#pragma once
#include <vector>
class BoardState
{
private:
std::vector<int> numbers;
int SIZE;
public:
BoardState();
std::vector<int> getState();
bool isZero();
};
And here's the implementation, in a separate file:
#include "BoardState.h"
BoardState::BoardState(){
SIZE = 4;
numbers.push_back(1);
numbers.push_back(3);
numbers.push_back(5);
numbers.push_back(7);
}
std::vector<int> BoardState::getState() { return numbers; }
bool BoardState::isZero() {
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] != 0) { return false; }
}
return true;
}
This code is really simple, so I have no clue what could be going wrong to produce the errors. However, on every method call, push_back and size, I am getting errors, saying that class "std::vector<int, allocator>" has no member "method_name_here".
My background is Java, so my first thought was that I wasn't able to call these methods because numbers is not initialized. However, any attempt I made to initialize numbers in the header file resulted in an error as well. I tried std::vector<int> numbers = { 1,3,5,7 };, I tried std::vector<int> numbers(4,0);, I even tried creating an array and constructing the vector from that. Not only did all those attempts cause errors, they also didn't fix the method calls either.
What am I missing? Do I need to initialize the vector, or is what I have in the header file enough? Any advice would be helpful here, since I can't find anything online about similar errors. I've even copy-pasted code from StackOverflow answers about similar problems, and that produced errors as well.
EDIT: I've pared down the code as much as possible while keeping the error:
#include <vector>
class BoardState
{
std::vector<int> numbers;
BoardState() { numbers.push_back(1); }
int getSize() {
int i = numbers.size();
return i;
}
};
On the line numbers.push_back(1);, my compiler underlines the token "push_back", and highlighting it reads:
class "std::vector<int, allocator>" has no member "push_back"
On the line int i = numbers.size();, the token "size" is underlined, and the error reads:
class "std::vector<int, allocator>" has no member "size"
I still have no clue what's going on.
Edit 2: Put the method calls into a constructor and a function. This changed the error message associated with push_back().
Edit 3: I have discovered something very disconcerting. This code works perfectly fine in a different compiler. I copy-pasted in the exact code from Edit 1 and it ran with no issues. I think the problem must be with Visual Studio rather than the actual code. Thank you all for helping me out with this. I think I'm just going to switch to a different compiler and hope for the best.
Edit 4: Just to prove to pm100 that my code is exactly as I've said, here's a screenshot from visual studio.
Here it is.
Aside from the main method, this is character-for-character what I've put in this question. I have a guess as to why this doesn't work, though. I modified my version of Visual Studio 2019 to run .386 assembly code for a college class. While I think I followed the guide to do that without affecting anything else, it may have screwed up parts of the C++ compiler.
I suggest that you could select Tools->Import and Export Settings->Reset all settings-> Visual C++ to restore the default settings.
If it does not work, you could reinstall VS.
One very experienced programmer wrote something like this:
#include <string>
using namespace std;
class Fraction
{
float nominator;
float denominator;
void load()
{
cin>>nominator; cin>>denominator;
if(denominator==0)
{
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
}
}
};
I have no idea why there is an if statement. Is it really necessary?
In this particular example,
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
would be exactly equivalent.
In the context you provided, nothing can tell us why someone would nest that loop in an useless if, but one could come up with explanations. In earlier version of that code, something could have been present inside the if block that changed the behavior of the program/function.
It also could be an innocent error.
Stroustrup's cat might have walked on their keyboard.
Is it really necessary?
No, it is not necessary.
The shown snippet is equivalent to a program where the if statement is replaced with the contained loop statement.
From this code:
if(denominator==0)
{
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
}
I think if is only used for a teaching purpose to simply show that how flow will go to while after checking if condition. And how while loop will work further.
But if is not really required here.
This code do the same:
while(denominator==0)
{
cout<<"denominator can not be equal 0!"<<endl;
cin>>denominator;
}
Okay, I'm sorry. It seems that Visual Studio just goes crazy and the values are REALLY fine. As to why this happens and when, I have no idea.
There is a complete sample at the end, I'd like to know if it does this crazy thing for you as well. Try it with x86 and x64 builds and look at what the debugger says the value is.
Maybe this is a good thing though, now I won't miss needless copying.
I find this really odd. Whenever I copy a glm::i64vec2 it gets corrupted.
But if i do just this line:
glm::i64vec2 copy(glm::i64vec2(1, 1));
In my main, it just works.
I have deleted all object files and rebuilt the program from scratch.. any ideas what might be wrong?
The only one I can think of is that I have included headers in different order in different places and that somehow messed this up.
What I really should use here is const reference, but still this scares the crap out of me. One of these could easily stay hidden as a bug and cause lots of headache for me.
I just found another bug this caused. This time it happens inside a single class.
This fails, but the same works in main
bool test(glm::i64vec2 p_worldPos)
{
return p_worldPos.x == 1;
}
void QuadTree::getObjectsHitInPriority(glm::i64vec2 const & p_worldPos, std::vector<Object*> & p_objects)
{
test(p_worldPos);
Um, I don't know what this means, but the following observation has been made. In my program main.cpp this fails
#include <glm/glm.hpp>
#include <iostream>
struct whatthehell
{
bool test(glm::u64vec2 p_test)
{
return p_test.x == 1;
}
};
int main(int argc, char ** argv)
{
whatthehell hell;
if (hell.test(glm::u64vec2(1, 1)) == false)
{
std::cout << "What the hell\n";
}
return 0;
}
Ok, update. This works with x86 but not on x64 with my VS2015. Can anyone test this?
I need help making an c++ program with a function that uses int Disc(int A, int B, int C) and calculates returns B*B-4*A*C and use the function Disc in the program..... i have this so far.
void main(){
cout << Disc(a,b,c);
}
What does the book you are using say about functions?
Assuming you can't get a book, take a look that this tutorial on functions (See the edit below)
You already have the function definition. The name, what parameters it takes and what it returns so if you take some time looking at the above tutorial you should be able to put something together (All you need to do is to write the body of the function).
The one thing that may cause you an issue (compiler error) is if you don't put it above the main function as either the function definition or the function itself must come before it is used. For simplicity at this point I reccomend you just put the function itself above the main function as shown in first example in the tutorial I linked to.
EDIT about linked tutorial
It suggests you use return with brackets. Example:
return (5);
Where as it should be used without them. Example:
return 5;
Ok, so you define the function Disc then:
int Disc(int A, int B, int C)
{
/* tricky part goes here... */
}