Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

C++ use of undeclared identifier

 
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

A few days ago my code was working fine until I started using boost. I installed boost by running a "brew install boost." I am now getting some compiler errors. Here is my code



here is my error list

CSE450Program.cpp:22:16: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
   int weight = 0;
              ^
CSE450Program.cpp:32:5: error: use of undeclared identifier 'flight'; did you mean 'right'?
   flight[std::make_pair(start,end)] = wt;
   ^~~~~~
   right
/Library/Developer/CommandLineTools/usr/include/c++/v1/ios:957:1: note: 'right' declared here
right(ios_base& __str)
^
CSE450Program.cpp:32:11: error: type 'std::__1::ios_base &(std::__1::ios_base &)' does not provide a subscript operator
   flight[std::make_pair(start,end)] = wt;
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
CSE450Program.cpp:33:5: error: use of undeclared identifier 'weight'
   weight = weight + wt;
   ^
CSE450Program.cpp:33:14: error: use of undeclared identifier 'weight'
   weight = weight + wt;
            ^
1 warning and 4 errors generated.

I have a feeling it is going to be something dumb I am overlooking but I have been banging my head over this for a few hours now. Thank you for your time and help!
 
Saloon Keeper
Posts: 11918
253
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use C++ 11 features (such as initializing a field when you're declaring it) you should use the compiler switch -std=c++11.

You didn't declare addEdge as a member of your Graph class when you tried to define it. You should declare it as void Graph::addEdge(string start, string end, int weight).

Note that it's conventional to put your class and member declarations in a header file, while putting the method definitions in an implementation file.

By the way, why did you make weight public instead of private?
 
Rhyeca Riley
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Stephan,

Thank you for the help! I know it was something small I was missing. I was planning on adding a header file after I got everything working on my cpp file. I know that might be bad coding practice but it has worked for me while I have been in school. As far as weight, I wanted to use it in my main class which is why I made it public.
 
Stephan van Hulst
Saloon Keeper
Posts: 11918
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the piece of pie!

Rhyeca Riley wrote:As far as weight, I wanted to use it in my main class which is why I made it public.


Well, don't. Otherwise you might as well use structs and free functions for everything. Why do you need to be able to reach into a graph to access the total weight of all edges directly? Why are you keeping track of the total weight, instead of just calculating it using another member function when you need it?
 
And inside of my fortune cookie was this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic